Problem: 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

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Stream;

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);
        solve(in, out);
        out.close();
    }

    static String reverse(String s) {
        return (new StringBuilder(s)).reverse().toString();
    }

    static void sieveOfEratosthenes(int n, int factors[]) 
    { 
        factors[1]=1;
        for(int p = 2; p*p <=n; p++) 
        { 
            if(factors[p] == 0) 
            { 
                factors[p]=p;
                for(int i = p*p; i <= n; i += p) 
                    factors[i] = p; 
            } 
        } 
    }

    static void sort(int ar[]) {
        int n = ar.length;
        ArrayList<Integer> a = new ArrayList<>();
        for (int i = 0; i < n; i++)
            a.add(ar[i]);
        Collections.sort(a);
        for (int i = 0; i < n; i++)
            ar[i] = a.get(i);
    }

    static void sort1(long ar[]) {
        int n = ar.length;
        ArrayList<Long> a = new ArrayList<>();
        for (int i = 0; i < n; i++)
            a.add(ar[i]);
        Collections.sort(a);
        for (int i = 0; i < n; i++)
            ar[i] = a.get(i);
    }

    static long ncr(long n, long r, long mod) {
        if (r == 0)
            return 1;
        long val = ncr(n - 1, r - 1, mod);
        val = (n * val) % mod;
        val = (val * modInverse(r, mod)) % mod;
        return val;
    }

    public static void solve(InputReader sc, PrintWriter pw) {
        int i, j = 0;
        int t = 1;  
        // int t = sc.nextInt();
        u: while (t-- > 0) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            Pair p[] = new Pair[m];
            for(i=0;i<m;i++){
                p[i] = new Pair(sc.nextInt(),sc.nextInt(),sc.nextInt());
            }
            Arrays.sort(p);
            int l = 1, r = m;
            int min = -1;
            while(l<=r){
                int mid = (l+r)/2;
                ArrayList<Integer> adj[] = new ArrayList[2*n+1];
                for(i=1;i<=2*n;i++){
                    adj[i] = new ArrayList<>();
                }
                for(i=0;i<mid;i++){
                    adj[p[i].a].add(p[i].b+n);
                    adj[p[i].b+n].add(p[i].a);
                }
                int val = bpm(adj,n);
                if(val==n){
                    min = p[mid-1].c;
                    // pw.println(min);
                    r = mid - 1;
                }
                else{
                    l = mid + 1;
                }
            }
            pw.println(min);
        }
    }

    static int bpm(ArrayList<Integer> adj[], int n){
        int pair[] = new int[2*n+1];
        int dist[] = new int[n+1];
        int match = 0;
        while(bfs(pair,dist,n,adj)){
            for(int v = 1; v <= n; v++){
                if(pair[v]==0 && dfs(v,pair,dist,n,adj)){
                    match++;
                }
            }
        }
        return match;
    }

    static boolean bfs(int pair[], int dist[], int n, ArrayList<Integer> adj[]){
        Deque<Integer> q = new LinkedList<>();
        for(int i=1;i<=n;i++){
            if(pair[i]==0){
                dist[i] = 0;
                q.offerLast(i);
            }
            else{
                dist[i] = Integer.MAX_VALUE;
            }
        }
        dist[0] = Integer.MAX_VALUE;
        while(q.size()>0){
            int v = q.pollFirst();
            if(dist[v]<dist[0]){
                for(int a:adj[v]){
                    if(dist[pair[a]]==Integer.MAX_VALUE){
                        dist[pair[a]] = dist[v] + 1;
                        q.offerLast(pair[a]);
                    }
                }
            }
        }
        return dist[0]!=Integer.MAX_VALUE;
    }

    static boolean dfs(int v, int pair[], int dist[], int n, ArrayList<Integer> adj[]){
        if(v==0)
            return true;
        for(int a:adj[v]){
            if(dist[pair[a]]==dist[v]+1){
                if(dfs(pair[a],pair,dist,n,adj)){
                    pair[a] = v;
                    pair[v] = a;
                    return true;
                }
            }
        }
        dist[v] = Integer.MAX_VALUE;
        return false;
    }

    static class Pair implements Comparable<Pair> {
        int a;
        int b;
        int c;
        
        Pair(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
 
        public int compareTo(Pair p) {
            // if(a!=p.a)
            //     return a-p.a;
            return c-p.c;
        }
    }

    static boolean isPrime(long n) {
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
        if (n % 2 == 0 || n % 3 == 0)
            return false;
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
        return true;
    }

    static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    static long fast_pow(long base, long n, long M) {
        if (n == 0)
            return 1;
        if (n == 1)
            return base % M;
        long halfn = fast_pow(base, n / 2, M);
        if (n % 2 == 0)
            return (halfn * halfn) % M;
        else
            return (((halfn * halfn) % M) * base) % M;
    }

    static long modInverse(long n, long M) {
        return fast_pow(n, M - 2, M);
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}