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 PYTHON3):

Solution:
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# ------------------------------
 
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
    print(' '.join(map(str,l)))
# import sys
# sys.setrecursionlimit(5010)
# from heapq import *
# from collections import deque as dq
# from math import ceil,floor,sqrt,pow
# import bisect as bs
# from collections import Counter
from collections import defaultdict as dc 

def judge(last,key,d1,d2):
    if key>last:
        for (a,b,c) in data:
            if last<c<=key:
                d1[a].add(b)
                d2[b].add(a)

    else:
        for (a,b,c) in data:
            if key<c<=last:
                d1[a].remove(b)
                if not d1[a]:
                    del d1[a]
                d2[b].remove(a)
                if not d2[b]:
                    del d2[b]
    if len(d1.keys())<n or len(d2.keys())<n:
        return False

    for a in d1:
        lone = 0
        for b in d1[a]:
            if len(d2[b])==1:
                lone+=1
                if lone>1:
                    return False
    for b in d2:
        lone = 0
        for a in d2[b]:
            if len(d1[a])==1:
                lone+=1
                if lone>1:
                    return False

        
    used = set()
    for a in d1:
        if a not in used:
            s1,s2,now = {a},d1[a].copy(),d1[a].copy()
            while now:
                b = now.pop()
                da = d2[b]-s1 
                s1|=da 
                for aa in da:
                    db = d1[aa]-s2
                    now|=db
                    s2|=db
            if len(s1)!=len(s2):
                return False
            used|=s1
    return True


n,m = RL()
data = []
for _ in range(m):
    data.append(tuple(map(int, sys.stdin.readline().split())))
l,r = min(a[2] for a in data),max(a[2] for a in data)+1
md = r
last = 0
du = dc(set)
dv = dc(set)
while l<r:
    key = (l+r)>>1
    if judge(last,key,du,dv):
        r = key
    else:
        l = key+1
    last = key
if l>=md:
    print(-1)
else:
    print(l)