import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            int haha = (int) 1e9;
            ArrayList<Integer> ar = new ArrayList<>();
            TreeSet<Integer> tset = new TreeSet<>();
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                tset.add(store[i][2]);
            }
            tset.add(haha);
            for (int xx : tset)
                ar.add(xx);
            DSU ob = new DSU(n);

            int lo = 0, hi = ar.size() - 1;
            while (lo <= hi) {
                int mid = (lo + hi) >> 1;
                PriorityQueue<Pair> pq = new PriorityQueue<>(com);
                pq.add(new Pair(ar.get(mid), 0));
                for (i = 1; i < m; i++)
                    pq.add(new Pair(store[i][2], i));
                ob.clear(n);
                boolean ok = false;
                while (pq.size() > 0) {
                    Pair hola = pq.poll();
                    int u = store[hola.y][0];
                    int v = store[hola.y][1];
                    long w = hola.x;
                    if (ob.find(u) != ob.find(v)) {
                        ob.unite(u, v);
                        if (hola.y == 0) {
                            ok = true;
                            break;
                        }
                    }
                }
                if (ok)
                    lo = mid + 1;
                else
                    hi = mid - 1;
            }
            pn(ar.get(lo - 1));
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void clear(int n) {
                make_set(n);
                Arrays.fill(rank, 0);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int[][] edges = new int[m][2];
            DSU ob = new DSU(n);
            Pair[] hola = new Pair[m];
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                if (i == 0) {
                    x = u;
                    y = v;
                }
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                hola[i] = new Pair(store[i][2], i);
            }
            Arrays.sort(hola, com);
            long wt = 0;
            for (i = 0; i < m; i++) {
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                if (u == x && v == y)
                    continue;
                long w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                    Edge e = new Edge(u, v, w);
                    wt = Math.max(w, wt);
                }
            }
            pn(wt);
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

        class Edge {
            int u;
            int v;
            long wt;

            Edge(int a, int b, long w) {
                u = a;
                v = b;
                wt = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int[][] edges = new int[m][2];
            DSU ob = new DSU(n);
            Pair[] hola = new Pair[m];
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                hola[i] = new Pair(store[i][2], i);
            }
            Arrays.sort(hola, com);
            long wt = 0;
            for (i = 0; i < m; i++) {
                if (hola[i].y == 0)
                    continue;
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                long w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                    Edge e = new Edge(u, v, w);
                    wt = Math.max(w, wt);
                }
            }

            ob = new DSU(n);
            int mn = (int) 1e9;
            int mx = 0;
            ArrayList<Integer> ar1 = new ArrayList<>();
            for (i = 0; i < m; i++) {
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                int w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                } else {
                    mn = Math.min(mn, w);
                    ar1.add(mn);
                }
                mx = Math.max(mx, w);
            }

            TreeSet<Integer> ar = new TreeSet<>();
            int cnt = 0;
            for (int xx : ar1) {
                if (xx > store[i][2]) {
                    ar.add(xx);
                    cnt++;
                }
                if (cnt == 5)
                    break;
            }
            ar.add(mn);
            ar.add(mx);
            ar.add((int) wt);
            ar.add(store[0][2]);
            ar.add((int) 1e9);
            int ans = 0;
            for (int xx : ar) {
                PriorityQueue<Pair> pq = new PriorityQueue<>(com);
                pq.add(new Pair(xx, 0));
                for (i = 1; i < m; i++)
                    pq.add(new Pair(store[i][2], i));
                ob.clear(n);
                boolean ok = false;
                while (pq.size() > 0) {
                    Pair yo = pq.poll();
                    int u = store[yo.y][0];
                    int v = store[yo.y][1];
                    if (ob.find(u) != ob.find(v)) {
                        ob.unite(u, v);
                        if (yo.y == 0) {
                            ok = true;
                            break;
                        }
                    }
                }
                if (ok)
                    ans = Math.max(ans, xx);
            }
            pn(Math.min(ans, (int) 1e9));

        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void clear(int n) {
                make_set(n);
                Arrays.fill(rank, 0);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

        class Edge {
            int u;
            int v;
            long wt;

            Edge(int a, int b, long w) {
                u = a;
                v = b;
                wt = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
            }
            int haha = (int) 1e9;
            int lo = 0, hi = haha;
            while (lo <= hi) {
                int mid = (lo + hi) >> 1;
                Pair[] hola = new Pair[m];
                hola[0] = new Pair(mid, 0);
                for (i = 1; i < m; i++)
                    hola[i] = new Pair(store[i][2], i);
                DSU ob = new DSU(n);
                Arrays.sort(hola, com);
                boolean ok = false;
                for (i = 0; i < m; i++) {
                    int u = store[hola[i].y][0];
                    int v = store[hola[i].y][1];
                    long w = hola[i].x;
                    if (ob.find(u) != ob.find(v)) {
                        ob.unite(u, v);
                        if (hola[i].y == 0)
                            ok = true;
                    }
                }
                if (ok)
                    lo = mid + 1;
                else
                    hi = mid - 1;
            }
            pn(lo - 1);
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int[][] edges = new int[m][2];
            DSU ob = new DSU(n);
            Pair[] hola = new Pair[m];
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                hola[i] = new Pair(store[i][2], i);
            }
            Arrays.sort(hola, com);
            long wt = 0;
            for (i = 0; i < m; i++) {
                if (hola[i].y == 0)
                    continue;
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                long w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                    Edge e = new Edge(u, v, w);
                    wt = Math.max(w, wt);
                }
            }

            ob = new DSU(n);
            int mn = (int) 1e9;
            int mx = 0;
            ArrayList<Integer> ar1 = new ArrayList<>();
            for (i = 0; i < m; i++) {
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                int w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                } else {
                    mn = Math.min(mn, w);
                    ar1.add(mn);
                }
                mx = Math.max(mx, w);
            }

            TreeSet<Integer> ar = new TreeSet<>();
            int cnt = 0;
            for (int xx : ar1) {
                if (xx > store[0][2]) {
                    ar.add(xx);
                    cnt++;
                }
                if (cnt == 5)
                    break;
            }
            ar.add(mn);
            ar.add(mx);
            ar.add((int) wt);
            ar.add(store[0][2]);
            ar.add((int) 1e9);
            int ans = 0;
            for (int xx : ar) {
                PriorityQueue<Pair> pq = new PriorityQueue<>(com);
                pq.add(new Pair(xx, 0));
                for (i = 1; i < m; i++)
                    pq.add(new Pair(store[i][2], i));
                ob.clear(n);
                boolean ok = false;
                while (pq.size() > 0) {
                    Pair yo = pq.poll();
                    int u = store[yo.y][0];
                    int v = store[yo.y][1];
                    if (ob.find(u) != ob.find(v)) {
                        ob.unite(u, v);
                        if (yo.y == 0) {
                            ok = true;
                            break;
                        }
                    }
                }
                if (ok)
                    ans = Math.max(ans, xx);
            }
            pn(Math.min(ans, (int) 1e9));

        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void clear(int n) {
                make_set(n);
                Arrays.fill(rank, 0);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

        class Edge {
            int u;
            int v;
            long wt;

            Edge(int a, int b, long w) {
                u = a;
                v = b;
                wt = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
            }
            DSU ob = new DSU(n);
            int haha = (int) 1e9;
            int lo = 0, hi = haha;
            while (lo <= hi) {
                int mid = (lo + hi) >> 1;
                Pair[] hola = new Pair[m];
                hola[0] = new Pair(mid, 0);
                for (i = 1; i < m; i++)
                    hola[i] = new Pair(store[i][2], i);
                ob.clear(n);
                Arrays.sort(hola, com);
                boolean ok = false;
                for (i = 0; i < m; i++) {
                    int u = store[hola[i].y][0];
                    int v = store[hola[i].y][1];
                    long w = hola[i].x;
                    if (ob.find(u) != ob.find(v)) {
                        ob.unite(u, v);
                        if (hola[i].y == 0)
                            ok = true;
                    }
                }
                if (ok)
                    lo = mid + 1;
                else
                    hi = mid - 1;
            }
            pn(lo - 1);
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void clear(int n) {
                make_set(n);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        long INF = (long) 1e18 + 1;
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int[][] edges = new int[m][2];
            DSU ob = new DSU(n);
            Pair[] hola = new Pair[m];
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                hola[i] = new Pair(store[i][2], i);
            }
            Arrays.sort(hola, com);
            long wt = 0;
            boolean[] taken = new boolean[m];
            for (i = 0; i < m; i++) {
                if (hola[i].y == 0)
                    continue;
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                long w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                    Edge e = new Edge(u, v, w);
                    wt = Math.max(w, wt);
                }
            }
            int haha = (int) 1e9;
            if (m == n - 1)
                pn(haha);
            else if (wt < store[0][2])
                pn(wt);
            else {
                ob = new DSU(n);
                long mn = INF;
                taken = new boolean[m];
                for (i = 0; i < m; i++) {
                    int u = store[hola[i].y][0];
                    int v = store[hola[i].y][1];
                    long w = hola[i].x;
                    if (ob.find(u) != ob.find(v)) {
                        ob.unite(u, v);
                        Edge e = new Edge(u, v, w);
                        wt = Math.max(w, wt);
                        taken[hola[i].y] = true;
                    } else
                        mn = Math.min(mn, store[hola[i].y][2]);
                }
                pn(mn);
            }
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

        class Edge {
            int u;
            int v;
            long wt;

            Edge(int a, int b, long w) {
                u = a;
                v = b;
                wt = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


//package math_codet;

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class lets_do {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        long INF = (long) 1e18 + 1;
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int[][] edges = new int[m][2];
            DSU ob = new DSU(n);
            Pair[] hola = new Pair[m];
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                if(i == 0) {
                	x = u;
                	y = v;
                }
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                hola[i] = new Pair(store[i][2], i);
                if(i == 0)
                	hola[i] = new Pair(111111111, 1111111111);
            }
            Arrays.sort(hola, com);
            long wt = 0;
            int haha = (int) 1e9;
            if (m == n - 1)
                pn(haha);
            else {
                ob = new DSU(n);
                long mn = INF;
                for (i = 0; i < m - 1; i++) {
                    int u = store[hola[i].y][0];
                    int v = store[hola[i].y][1];
                    long w = hola[i].x;
                    ob.unite(u, v);
                    if(ob.find(x) == ob.find(y))
                        mn = Math.min(mn, store[hola[i].y][2]);
                }
                pn(mn);
            }
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

        class Edge {
            int u;
            int v;
            long wt;

            Edge(int a, int b, long w) {
                u = a;
                v = b;
                wt = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Aman Kumar Singh
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        E1DaleksInvasionEasy solver = new E1DaleksInvasionEasy();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1DaleksInvasionEasy {
        PrintWriter out;
        InputReader in;
        int n;
        final Comparator<Pair> com = new Comparator<Pair>() {
            public int compare(Pair a, Pair b) {
                if (Integer.compare(a.x, b.x) != 0)
                    return Integer.compare(a.x, b.x);
                else
                    return Integer.compare(a.y, b.y);
            }
        };

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int m = ni();
            int[][] edges = new int[m][2];
            DSU ob = new DSU(n);
            Pair[] hola = new Pair[m];
            int i = 0;
            int[][] store = new int[m][3];
            int x = -1, y = -1;
            for (i = 0; i < m; i++) {
                int u = ni() - 1;
                int v = ni() - 1;
                store[i][0] = u;
                store[i][1] = v;
                store[i][2] = ni();
                hola[i] = new Pair(store[i][2], i);
            }
            Arrays.sort(hola, com);
            long wt = 0;
            for (i = 0; i < m; i++) {
                if (hola[i].y == 0)
                    continue;
                int u = store[hola[i].y][0];
                int v = store[hola[i].y][1];
                long w = hola[i].x;
                if (ob.find(u) != ob.find(v)) {
                    ob.unite(u, v);
                    Edge e = new Edge(u, v, w);
                    wt = Math.max(w, wt);
                }
            }
            if (m == n - 1)
                pn((int) 1e9);
            else
                pn(wt);
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        class DSU {
            int[] rank;
            int[] parent;

            public DSU(int n) {
                rank = new int[n];
                parent = new int[n];
                make_set(n);
            }

            void make_set(int n) {
                int i = 0;
                for (i = 0; i < n; i++)
                    parent[i] = i;
            }

            int find(int x) {
                if (parent[x] != x)
                    parent[x] = find(parent[x]);
                return parent[x];
            }

            void unite(int x, int y) {
                int xRoot = find(x), yRoot = find(y);
                if (xRoot == yRoot)
                    return;
                if (rank[xRoot] < rank[yRoot])
                    parent[xRoot] = yRoot;
                else if (rank[yRoot] < rank[xRoot])
                    parent[yRoot] = xRoot;
                else {
                    parent[yRoot] = xRoot;
                    rank[xRoot] = rank[xRoot] + 1;
                }
            }

        }

        class Pair {
            int x;
            int y;

            Pair(int p, int q) {
                x = p;
                y = q;
            }

        }

        class Edge {
            int u;
            int v;
            long wt;

            Edge(int a, int b, long w) {
                u = a;
                v = b;
                wt = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
        }

        @Override
        public int compareTo(edge o) {
            return w - o.w;
        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        int bs = bs(c1);
        System.out.println(bs>=1000000001?1000000000:bs);
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (st <= en) {
            int mid = st+(en- st) / 2;
            if (ok(mid)) {
                st = mid+1;
            } else {
                en = mid-1 ;
            }
        }
        int ans = Math.max(en,st);
        return ok(ans)?ans:Math.min(en,st);
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w,in;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first,int in) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
            this.in = in;
        }

        @Override
        public int compareTo(edge o) {
            if(w!=o.w)
                return w - o.w;
            else
                return in-o.in;

        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false,i));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        int bs = bs(c1);
        System.out.println(bs>=1000000001?1000000000:bs);
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (en-st>0) {
            int mid = st+(en- st+1) / 2;
            if (ok(mid)) {
                st = mid;
            } else {
                en = mid-1 ;
            }
        }
        int ans = Math.max(en,st);
        return ans;
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w,in;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first,int in) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
            this.in = in;
        }

        @Override
        public int compareTo(edge o) {
            if(w!=o.w)
                return w - o.w;
            else
                return in-o.in;

        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false,i));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        int bs = bs(c1);
        System.out.println(bs>=1000000001?1000000000:bs);
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (en-st>0) {
            int mid = st+(en- st+1) / 2;
            if (ok(mid)) {
                st = mid;
            } else {
                en = mid-1 ;
            }
        }
        int ans = Math.max(en,st);
        return ans;
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
    private static class Scanner{
        public BufferedReader reader;
        public StringTokenizer st;

        public Scanner(InputStream stream){
            reader = new BufferedReader(new InputStreamReader(stream));
            st = null;
        }

        public String next(){
            while(st == null || !st.hasMoreTokens()){
                try{
                    String line  = reader.readLine();
                    if(line == null) return null;
                    st =  new StringTokenizer(line);
                }catch (Exception e){
                    throw (new RuntimeException());
                }
            }
            return st.nextToken();
        }

        public int nextInt(){
            return Integer.parseInt(next());
        }
        public long nextLong(){
            return Long.parseLong(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
        }

        @Override
        public int compareTo(edge o) {
            return w - o.w;
        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        System.out.println(bs(c1));
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (st < en) {
            int mid = st+(en- st+1) / 2;
            if (ok(mid)) {
                st = mid+1;
            } else {
                en = mid-1 ;
            }
        }
        return Math.max(en,st);
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
        }

        @Override
        public int compareTo(edge o) {
            return w - o.w;
        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        System.out.println(bs(c1));
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (st < en) {
            int mid = st+(en- st+1) / 2;
            if (ok(mid)) {
                st = mid+1;
            } else {
                en = mid ;
            }
        }
        return en;
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
        }

        @Override
        public int compareTo(edge o) {
            return w - o.w;
        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        int bs = bs(c1);
        System.out.println(bs>=1000000001?1000000000:bs);
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (en-st>0) {
            int mid = st+(en- st+1) / 2;
            if (ok(mid)) {
                st = mid;
            } else {
                en = mid-1 ;
            }
        }
        int ans = Math.max(en,st);
        return ok(ans+1)?ans+1:ans;
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class E1 {
    private static class kruskal {
        int[] parent, rank;
        int count;

        public kruskal(int n) {
            this.count = n;
            parent = new int[ n ];
            rank = new int[ n ];
            for ( int i = 0; i < n; i++ ) {
                parent[ i ] = i;
            }
        }

        public int find(int x) {
            return parent[ x ] == x ? x : (parent[ x ] = find(parent[ x ]));
        }

        public void union(int x, int y) {
            int repx = find(x), repy = find(y);
            if (repx == repy)
                return;
            if (rank[ repx ] < rank[ repy ]) {
                parent[ repx ] = repy;
            } else if (rank[ repy ] < rank[ repx ])
                parent[ repy ] = repx;
            else {
                parent[ repy ] = repx;
                rank[ repx ]++;
            }
        }
    }

    private static class edge implements Comparable<edge> {
        int f, t, w;
        boolean isFirst;

        public edge(int f, int t, int w, boolean first) {
            this.f = f;
            this.t = t;
            this.w = w;
            this.isFirst = first;
        }

        @Override
        public int compareTo(edge o) {
            return w - o.w;
        }

    }

    static int n, m;
    static ArrayList<edge> edgeList;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edgeList = new ArrayList<>();
        boolean flag = true;
        int c1 = 0;
        for ( int i = 0; i < m; i++ ) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int w = sc.nextInt();
            edgeList.add(new edge(f, t, w, flag ? true : false));
            if (flag) {
                c1 = w;
                flag = false;
            }

        }
        int bs = bs(c1);
        System.out.println(bs>=1000000001?100000000:bs);
    }

    static int bs(int c1) {
        int st = 0;
        int en = (int)1e9;
        while (st <= en) {
            int mid = st+(en- st) / 2;
            if (ok(mid)) {
                st = mid+1;
            } else {
                en = mid-1 ;
            }
        }
        int ans = Math.max(en,st);
        return ok(ans)?ans:Math.min(en,st);
    }

    static boolean ok(int mid) {
       for ( int i = 0;i < edgeList.size();i++){
           if(edgeList.get(i).isFirst){
               edgeList.get(i).w = mid;
           }
       }
       Collections.sort(edgeList);
       kruskal krus = new kruskal(n);
       int nb = 0;
       boolean check = false;
       for ( int i = 0;i < edgeList.size();i++){
           edge temp = edgeList.get(i);
           if(krus.find(temp.f)!=krus.find(temp.t)){
               krus.union(temp.f,temp.t);
               if(temp.isFirst){
                  check = true;
               }
               nb++;
           }
       }
       return check&&nb==n-1;

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Rustam Musin (t.me/musin_acm)
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        E1VtorzhenieDalekovLegkaya solver = new E1VtorzhenieDalekovLegkaya();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1VtorzhenieDalekovLegkaya {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int m = in.readInt();
            int a = -1, b = -1;
            Edge[] edges = new Edge[m];
            for (int i = 0; i < m; i++) {
                int x = in.readInt() - 1;
                int y = in.readInt() - 1;
                int c = in.readInt();
                if (i == 0) {
                    a = x;
                    b = y;
                }
                edges[i] = new Edge(x, y, c);
            }
            Arrays.sort(edges);
            DSU dsu = new DSU(n);
            for (Edge e : edges) {
                dsu.unite(e.a, e.b);
                if (dsu.find(a) == dsu.find(b)) {
                    out.print(e.cost);
                    return;
                }
            }
        }

        class DSU {
            int[] parent;

            DSU(int n) {
                parent = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                }
            }

            int find(int v) {
                if (v == parent[v]) {
                    return v;
                }
                return parent[v] = find(parent[v]);
            }

            void unite(int u, int v) {
                parent[find(u)] = find(v);
            }

        }

        class Edge implements Comparable<Edge> {
            int a;
            int b;
            int cost;

            Edge(int a, int b, int cost) {
                this.a = a;
                this.b = b;
                this.cost = cost;
            }

            public int compareTo(Edge o) {
                return Integer.compare(cost, o.cost);
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void print(int i) {
            writer.print(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Rustam Musin (t.me/musin_acm)
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        E1VtorzhenieDalekovLegkaya solver = new E1VtorzhenieDalekovLegkaya();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1VtorzhenieDalekovLegkaya {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int m = in.readInt();
            int a = -1, b = -1;
            Edge[] edges = new Edge[m];
            for (int i = 0; i < m; i++) {
                int x = in.readInt() - 1;
                int y = in.readInt() - 1;
                int c = in.readInt();
                if (i == 0) {
                    a = x;
                    b = y;
                }
                edges[i] = new Edge(x, y, c);
            }
            Arrays.sort(edges);
            DSU dsu = new DSU(n);
            for (Edge e : edges) {
                if (e.a == a && e.b == b) {
                    continue;
                }
                dsu.unite(e.a, e.b);
                if (dsu.find(a) == dsu.find(b)) {
                    out.print(e.cost);
                    return;
                }
            }
            out.print((int) 1e9);
        }

        class DSU {
            int[] parent;

            DSU(int n) {
                parent = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                }
            }

            int find(int v) {
                if (v == parent[v]) {
                    return v;
                }
                return parent[v] = find(parent[v]);
            }

            void unite(int u, int v) {
                parent[find(u)] = find(v);
            }

        }

        class Edge implements Comparable<Edge> {
            int a;
            int b;
            int cost;

            Edge(int a, int b, int cost) {
                this.a = a;
                this.b = b;
                this.cost = cost;
            }

            public int compareTo(Edge o) {
                return Integer.compare(cost, o.cost);
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void print(int i) {
            writer.print(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e6+5;
struct node{
	ll f,t,v;
}a[maxn];
ll fa[maxn];
ll getfa(ll v)
{
	if(fa[v]!=v) fa[v]=getfa(fa[v]);
	return fa[v];
}
bool cmp(node a,node b)
{
	return a.v<b.v;
}
int main()
{
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=m;i++)
	{
		ll f,t,v;
		scanf("%lld%lld%lld",&a[i].f,&a[i].t,&a[i].v);
	}
	node temp=a[1];
	sort(a+2,a+1+m,cmp);
//	for(int i=2;i<=n;i++) cout<<a[i].f<<" "<<a[i].t<<" "<<a[i].v<<endl;
	ll ans=0;
	int tot=0;
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=2;i<=m;i++)
	{
		ll x=getfa(a[i].f);
		ll y=getfa(a[i].t);
		if(x==y) continue;
		else ans+=a[i].v,tot++;
		fa[x]=y;
	}
	//cout<<ans<<" "<<endl;
	if(tot<n-1) printf("1000000000\n");
	else
	{
		for(int i=1;i<=n;i++) fa[i]=i;
		ll ans1=temp.v;
		fa[temp.f]=temp.t;
		for(int i=2;i<=n;i++)
		{
			ll x=getfa(a[i].f);
			ll y=getfa(a[i].t);
			if(x==y) continue;
			else ans1+=a[i].v;
			fa[x]=y;
		}
		//cout<<ans1<<" "<<endl;
		if(ans1>=ans) printf("%lld\n",temp.v+ans-ans1);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxm= 1e6+10;
const int Maxn = 1e5+10;
ll fa[Maxn],fa2[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
ll find2(ll x){
	if(x == fa2[x]) return x;
	else return fa2[x] = find(fa2[x]);
}
struct EE{
	ll x,y,z;
}node[Maxm],first;
bool cmp(EE a,EE b){
	return a.z < b.z;
}
int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
		}
		first = node[0];
		for(ll i=0;i<n;i++) fa[i]=i,fa2[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		fa2[first.x] = first.y;
		tol=0;
		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find2(node[i].x);
			ll v = find2(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node[i].z;
			}
		//	cout<<" (((((("<<endl;
			if(tol==n-2) break;
		}
		if(ans2 <= ans1) {
			printf("1000000000\n");
		}
		else {
			printf("%lld\n",first.z-(ans2-ans1));
		}
	}
	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxn= 1e6+10;
ll fa[Maxn],fa2[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
ll find2(ll x){
	if(x == fa2[x]) return x;
	else return fa2[x] = find(fa2[x]);
}
struct EE{
	ll x,y,z;
}node[Maxn],first,node2[Maxn];
bool cmp(EE a,EE b){
	return a.z < b.z;
}
int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
			if(i!=0)node2[i] =node[i];
		}
		first = node[0];
		for(ll i=0;i<n;i++) fa[i]=i,fa2[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;

		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		fa2[first.x] = first.y;
		sort(node2+1,node2+m,cmp);
		tol=0;
		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find2(node2[i].x);
			ll v = find2(node2[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node2[i].z;
			}
		//	cout<<" (((((("<<endl;
			if(tol==n-2) break;
		}
		if(ans2 <= ans1) {
			printf("1000000000\n");
		}
		else {
			printf("%lld\n",first.z-(ans2-ans1));
		}
	}
	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxn= 1e6+10;
const int Maxm = 1e5+10;
ll fa[Maxn],fa2[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
ll find2(ll x){
	if(x == fa2[x]) return x;
	else return fa2[x] = find(fa2[x]);
}
struct EE{
	ll x,y,z;
}node[Maxm],first;
bool cmp(EE a,EE b){
	return a.z < b.z;
}
int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
		}
		first = node[0];
		for(ll i=0;i<n;i++) fa[i]=i,fa2[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		fa2[first.x] = first.y;
		tol=0;
		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find2(node[i].x);
			ll v = find2(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node[i].z;
			}
		//	cout<<" (((((("<<endl;
			if(tol==n-2) break;
		}
		if(ans2 <= ans1) {
			printf("1000000000\n");
		}
		else {
			printf("%lld\n",first.z-(ans2-ans1));
		}
	}
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e6+5;
struct node{
	ll f,t,v;
}a[maxn];
ll fa[maxn];
ll getfa(ll v)
{
	if(fa[v]!=v) fa[v]=getfa(fa[v]);
	return fa[v];
}
bool cmp(node a,node b)
{
	return a.v<b.v;
}
int main()
{
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=m;i++)
	{
		ll f,t,v;
		scanf("%lld%lld%lld",&a[i].f,&a[i].t,&a[i].v);
	}
	node temp=a[1];
	sort(a+2,a+1+m,cmp);
//	for(int i=2;i<=n;i++) cout<<a[i].f<<" "<<a[i].t<<" "<<a[i].v<<endl;
	ll ans=0;
	int tot=0;
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=2;i<=m;i++)
	{
		ll x=getfa(a[i].f);
		ll y=getfa(a[i].t);
		if(x==y) continue;
		else ans+=a[i].v,tot++,fa[x]=y;;

	}
	//cout<<ans<<" "<<endl;
	if(tot<n-1) printf("1000000000\n");
	else
	{
		for(int i=1;i<=n;i++) fa[i]=i;
		ll ans1=temp.v;
		fa[temp.f]=temp.t;
		for(int i=2;i<=m;i++)
		{
			ll x=getfa(a[i].f);
			ll y=getfa(a[i].t);
			if(x==y) continue;
			else ans1+=a[i].v,fa[x]=y;;
		}
		//cout<<ans1<<" "<<endl;
		if(ans1>=ans) printf("%lld\n",temp.v+ans-ans1);
	}
	return 0;
}


#include<bits/stdc++.h>
	using namespace std;
	typedef long long ll;
	const ll maxn=1e6+5;
	struct node{
		ll f,t,v;
	}a[maxn];
	ll fa[maxn];
	ll getfa(ll v)
	{
		if(fa[v]!=v) fa[v]=getfa(fa[v]);
		return fa[v];
	}
	bool cmp(node a,node b)
	{
		return a.v<b.v;
	}
	int main()
	{
		ll n,m;
		scanf("%lld%lld",&n,&m);
		for(int i=1;i<=n;i++)
		{
			ll f,t,v;
			scanf("%lld%lld%lld",&a[i].f,&a[i].t,&a[i].v);
		}
		node temp=a[1];
		sort(a+2,a+1+n,cmp);
	//	for(int i=2;i<=n;i++) cout<<a[i].f<<" "<<a[i].t<<" "<<a[i].v<<endl;
		ll ans=0;
		for(int i=1;i<=n;i++) fa[i]=i;
		for(int i=2;i<=n;i++)
		{
			ll x=getfa(a[i].f);
			ll y=getfa(a[i].t);
			if(x==y) continue;
			else ans+=a[i].v;
			fa[x]=y;
		}
		//cout<<ans<<" "<<endl;
		int flag=0;
		//for(int i=1;i<=n;i++) cout<<i<<" "<<fa[i]<<endl;
		for(int i=1;i<=n;i++)
		if(fa[i]==i)
		{
			flag++;
			if(flag>1)
			{
				printf("%lld\n",1e9);
				flag=0;
				break;
			}
		}
		if(flag)
		{
			for(int i=1;i<=n;i++) fa[i]=i;
			ll ans1=temp.v;
			fa[temp.f]=temp.t;
			for(int i=2;i<=n;i++)
			{
				ll x=getfa(a[i].f);
				ll y=getfa(a[i].t);
				if(x==y) continue;
				else ans1+=a[i].v;
				fa[x]=y;
			}
			//cout<<ans1<<" "<<endl;
			if(ans1>=ans) printf("%lld\n",temp.v+ans-ans1);
		}
		return 0;
	}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e6+5;
struct node{
	ll f,t,v;
}a[maxn];
ll fa[maxn];
ll getfa(ll v)
{
	if(fa[v]!=v) fa[v]=getfa(fa[v]);
	return fa[v];
}
bool cmp(node a,node b)
{
	return a.v<b.v;
}
int main()
{
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(ll i=1;i<=m;i++)
	{
		scanf("%lld%lld%lld",&a[i].f,&a[i].t,&a[i].v);
	}
	node temp=a[1];
	sort(a+2,a+1+m,cmp);
//	for(int i=2;i<=n;i++) cout<<a[i].f<<" "<<a[i].t<<" "<<a[i].v<<endl;
	ll ans=0;
	ll tot=0;
	for(ll i=1;i<=n;i++) fa[i]=i;
	for(ll i=2;i<=m;i++)
	{
		ll x=getfa(a[i].f);
		ll y=getfa(a[i].t);
		if(x==y) continue;
		else ans+=a[i].v,tot++,fa[x]=y;;

	}
	//cout<<ans<<" "<<endl;
	if(tot<n-1) printf("1000000000\n");
	else
	{
		for(ll i=1;i<=n;i++) fa[i]=i;
		ll ans1=temp.v;
		fa[temp.f]=temp.t;
		for(ll i=2;i<=m;i++)
		{
			ll x=getfa(a[i].f);
			ll y=getfa(a[i].t);
			if(x==y) continue;
			else ans1+=a[i].v,fa[x]=y;;
		}
		//cout<<ans1<<" "<<endl;
		if(ans1>=ans) printf("%lld\n",temp.v+ans-ans1);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxm= 1e6+10;
const int Maxn = 1e5+10;
ll fa[Maxn],fa2[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
struct EE{
	ll x,y,z;
}node[Maxm],first;
bool cmp(EE a,EE b){
	return a.z < b.z;
}
int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
		}
		first = node[0];
		for(ll i=0;i<n;i++) fa[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		fa2[first.x] = first.y;
		tol=0;
		for(ll i=0;i<n;i++) fa[i]=i;
		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node[i].z;
			}
		//	cout<<" (((((("<<endl;
			if(tol==n-2) break;
		}
		if(ans2 <= ans1) {
			printf("1000000000\n");
		}
		else {
			printf("%lld\n",first.z-(ans2-ans1));
		}
	}
	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxm= 1e6+10;
const int Maxn = 1e5+10;
ll fa[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
struct EE{
	ll x,y,z;
}node[Maxm],first;
bool cmp(EE a,EE b){
	return a.z < b.z;
}

int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			//if(i==0) scanf("%lld%lld%lld",&first.x,&first.y,&first.z);
			 scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
		}
		first = node[0];
		for(ll i=0;i<n;i++) fa[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		for(ll i=0;i<n;i++) fa[i]=i;
		fa[first.x] = first.y;
		tol=0;

		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node[i].z;
			}
			if(tol==n-2) break;
		}

		printf("%lld\n",first.z-(ans2-ans1));

	}
	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxm= 1e6+10;
const int Maxn = 1e5+10;
ll fa[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
struct EE{
	ll x,y,z;
}node[Maxm],first;
bool cmp(EE a,EE b){
	return a.z < b.z;
}
int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			//if(i==0) scanf("%lld%lld%lld",&first.x,&first.y,&first.z);
			 scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
		}
		first = node[0];
		for(ll i=0;i<=n;i++) fa[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		for(ll i=0;i<=n;i++) fa[i]=i;
		fa[first.x] = first.y;
		tol=0;

		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node[i].z;
			}
			if(tol==n-2) break;
		}

		printf("%lld\n",first.z-(ans2-ans1));

	}
	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Maxm= 1e6+10;
const int Maxn = 1e5+10;
ll fa[Maxn];
ll find(ll x){
	if(x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
struct EE{
	ll x,y,z;
}node[Maxm],first;
bool cmp(EE a,EE b){
	return a.z < b.z;
}
int main(){
	ll n,m;
	while(~scanf("%lld%lld",&n,&m)){
		for(ll i=0;i<m;i++){
			scanf("%lld%lld%lld",&node[i].x,&node[i].y,&node[i].z);
		}
		first = node[0];
		for(ll i=0;i<n;i++) fa[i]=i;
		sort(node+1,node+m,cmp);
		ll tol=0;
		ll ans1 = 0;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans1 += node[i].z;
			}
			if(tol==n-1) break;
		}
		if(tol != n-1) {
			puts("1000000000");
			continue;
		}
		fa[first.x] = first.y;
		tol=0;
		for(ll i=0;i<n;i++) fa[i]=i;
		ll ans2=first.z;
		for(ll i=1;i<m;i++){
			ll u = find(node[i].x);
			ll v = find(node[i].y);
			if(u !=v){
				fa[u] = v;
				tol++;
				ans2 += node[i].z;
			}
		//	cout<<" (((((("<<endl;
			if(tol==n-2) break;
		}
		if(ans2 <= ans1) {
			printf("1000000000\n");
		}
		else {
			printf("%lld\n",first.z-(ans2-ans1));
		}
	}
	return 0;

}