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.*;
import java.util.*;
public class Main {
    public static void main(String args[])
    {
        FastReader input=new FastReader();
        PrintWriter out=new PrintWriter(System.out);
        int T=1;
        while(T-->0)
        {
            int n=input.nextInt();
            int m=input.nextInt();
            ArrayList<Pair> adj[]=new ArrayList[n+1];
            for(int i=1;i<=n;i++)
            {
                adj[i]=new ArrayList<>();
            }
            int max=0;
            for(int i=0;i<m;i++)
            {
                int u=input.nextInt();
                int v=input.nextInt();
                int d=input.nextInt();
                max=Math.max(max,d);
                adj[v].add(new Pair(u,d));
            }
            int ans=binarFun(adj,n,max);
            if(ans>max)
            {
                out.println(-1);
            }
            else
            {
                out.println(ans);
            }
        }
        out.close();
    }
    public static int binarFun(ArrayList<Pair> adj[],int n,int max)
    {
        int i=1,j=max+1;
        while(i<j)
        {
            int mid=(i+j)/2;
            boolean isPass=fun(adj,mid,n);
            if(isPass)
            {
                j=mid;
            }
            else
            {
                i=mid+1;
            }
        }
        return i;
    }
    public static boolean fun(ArrayList<Pair> adj[],int val,int n)
    {
        HopcroftKarp g=new HopcroftKarp(n,n);
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<adj[i].size();j++)
            {
                int u=adj[i].get(j).u;
                int v=i;
                int d=adj[i].get(j).d;
                if(d<=val)
                {
                    g.addEdge(u,v);
                }
            }
        }
        int c=g.maxFlow();
        if(c==n)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    class Edge
    {
        int u,v,w;
        Edge(int u,int v,int w)
        {
            this.u=u;
            this.v=v;
            this.w=w;
        }
    }
    static class HopcroftKarp
    {
        int m,n;
        ArrayList<Integer> list[];
        int pair_u[],pair_v[],dist[];
        HopcroftKarp(int m,int n)
        {
            this.m=m;
            this.n=n;
            list=new ArrayList[m+1];
            for(int i=1;i<=m;i++)
                list[i]=new ArrayList<>();
            pair_u=new int[m+1];
            pair_v=new int[n+1];
            dist=new int[m+1];
        }
        void addEdge(int u,int v)
        {
            list[u].add(v);
        }
        int maxFlow()
        {
            int res=0;
            while(bfs())
            {
                for(int u=1;u<=m;u++)
                    if(pair_u[u]==0 && dfs(u))
                        res++;
            }
            return res;
        }
        boolean bfs()
        {
            Queue<Integer> queue=new LinkedList<>();
            for(int u=1;u<=m;u++)
                if(pair_u[u]==0)
                {
                    dist[u]=0;
                    queue.add(u);
                }
                else
                    dist[u]=1<<30;
            boolean hasPath=false;
            while(!queue.isEmpty())
            {
                int u=queue.remove();
                for(int v:list[u])
                    if(pair_v[v]==0)
                        hasPath=true;
                    else if(dist[pair_v[v]]>dist[u]+1)
                    {
                        dist[pair_v[v]]=dist[u]+1;
                        queue.add(pair_v[v]);
                    }
            }
            return hasPath;
        }
        boolean dfs(int u)
        {
            for(int v:list[u])
                if(pair_v[v]==0 || dist[pair_v[v]]==dist[u]+1 && dfs(pair_v[v]))
                {
                    pair_v[v]=u;
                    pair_u[u]=v;
                    return true;
                }
            return false;
        }
    }
    public static void mergeSort(HashSet<Integer> a[],int p,int r)
    {
        if(p<r)
        {
            int q=(p+r)/2;
            mergeSort(a,p,q);
            mergeSort(a,q+1,r);
            merge(a,p,q,r);
        }
    }
    public static void merge(HashSet<Integer> a[],int p,int q,int r)
    {
        int n1=q-p+1;
        HashSet<Integer> L[]=new HashSet[n1];
        int n2=r-q;
        HashSet<Integer> R[]=new HashSet[n2];
        for(int i=p;i<=q;i++)
        {
            L[i-p]=a[i];
        }
        for(int i=q+1;i<=r;i++)
        {
            R[i-q-1]=a[i];
        }
        int x=0,y=0;
        int i=p;
        while(x<n1 && y<n2)
        {
            if(L[x].size()>=R[y].size())
            {
                a[i]=L[x];
                x++;
            }
            else
            {
                a[i]=R[y];
                y++;
            }
            i++;
        }
        while(x<n1)
        {
            a[i]=L[x];
            x++;
            i++;
        }
        while(y<n2)
        {
            a[i]=R[y];
            y++;
            i++;
        }
    }
    public static class Pair
    {
        int u,d;
        Pair(int u,int d)
        {
            this.u=u;
            this.d=d;
        }
    }
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;
        public FastReader()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt()
        {
            return Integer.parseInt(next());
        }
        long nextLong()
        {
            return Long.parseLong(next());
        }
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
        String nextLine()
        {
            String str="";
            try
            {
                str=br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}