The pandemic is upon us, and the world is in shortage of the most important resource: toilet paper. As one of the best prepared nations for this crisis, BubbleLand promised to help all other world nations with this valuable resource. To do that, the country will send airplanes to other countries carrying toilet paper.

In BubbleLand, there are N toilet paper factories, and N airports. Because of how much it takes to build a road, and of course legal issues, every factory must send paper to only one airport, and every airport can only take toilet paper from one factory.

Also, a road can't be built between all airport-factory pairs, again because of legal issues. Every possible road has number d given, number of days it takes to build that road.

Your job is to choose N factory-airport pairs, such that if the country starts building all roads at the same time, it takes the least amount of days to complete them.

Input

The first line contains two integers N (1 ≤ N ≤ 10^4) - number of airports/factories, and M (1 ≤ M ≤ 10^5) - number of available pairs to build a road between.

On next M lines, there are three integers u, v (1 ≤ u,v ≤ N), d (1 ≤ d ≤ 10^9) - meaning that you can build a road between airport u and factory v for d days.

Output

If there are no solutions, output -1. If there exists a solution, output the minimal number of days to complete all roads, equal to maximal d among all chosen roads.

Example

Input


3 5
1 2 1
2 3 2
3 3 3
2 1 4
2 2 5


Output


4
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.OptionalInt;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public static int maxMatching(List<Integer>[] graph) {
            int n1 = graph.length;
            int n2 = Arrays.stream(graph).flatMap(Collection::stream).mapToInt(Integer::intValue).max().orElse(-1) + 1;
            int[] dist = new int[n1];
            int[] matching = new int[n2];
            Arrays.fill(matching, -1);
            boolean[] used = new boolean[n1];
            for (int res = 0; ; ) {
                bfs(graph, used, matching, dist);
                boolean[] vis = new boolean[n1];
                int f = 0;
                for (int u = 0; u < n1; ++u)
                    if (!used[u] && dfs(graph, vis, used, matching, dist, u))
                        ++f;
                if (f == 0)
                    return res;
                res += f;
            }
        }

        static void bfs(List<Integer>[] graph, boolean[] used, int[] matching, int[] dist) {
            Arrays.fill(dist, -1);
            int n1 = graph.length;
            int[] Q = new int[n1];
            int sizeQ = 0;
            for (int u = 0; u < n1; ++u) {
                if (!used[u]) {
                    Q[sizeQ++] = u;
                    dist[u] = 0;
                }
            }
            for (int i = 0; i < sizeQ; i++) {
                int u1 = Q[i];
                for (int v : graph[u1]) {
                    int u2 = matching[v];
                    if (u2 >= 0 && dist[u2] < 0) {
                        dist[u2] = dist[u1] + 1;
                        Q[sizeQ++] = u2;
                    }
                }
            }
        }

        static boolean dfs(List<Integer>[] graph, boolean[] vis, boolean[] used, int[] matching, int[] dist, int u1) {
            vis[u1] = true;
            for (int v : graph[u1]) {
                int u2 = matching[v];
                if (u2 < 0 || !vis[u2] && dist[u2] == dist[u1] + 1 && dfs(graph, vis, used, matching, dist, u2)) {
                    matching[v] = u1;
                    used[u1] = true;
                    return true;
                }
            }
            return false;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();

            ArrayList<TaskB.Edge> E = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                int u = in.nextInt() - 1, v = in.nextInt() - 1, w = in.nextInt();
                E.add(new TaskB.Edge(u, v, w));
            }
            Collections.sort(E, new TaskB.EdgeSorter());
            int L = 0, R = m;
            while (L < R) {
                int mid = (L + R) / 2;
                List<Integer>[] graph = Stream.generate(ArrayList::new).limit(n).toArray(List[]::new);
                for (int i = 0; i <= mid; i++) {
                    graph[E.get(i).u].add(E.get(i).v);
                }
                int mm = maxMatching(graph);
                if (mm == n) R = mid;
                else L = mid + 1;
            }
            out.println(L == m ? -1 : E.get(L).w);
        }

        static class Edge {
            int u;
            int v;
            int w;

            public Edge(int a, int b, int c) {
                u = a;
                v = b;
                w = c;
            }

        }

        static class EdgeSorter implements Comparator<TaskB.Edge> {

            public int compare(TaskB.Edge edge, TaskB.Edge t1) {
                return edge.w - t1.w;
            }

        }

    }

    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 nextInt() {
            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);

        }

    }

    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 println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
            writer.print('\n');
        }

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

    }
}

