import sys input = sys.stdin.readline N,M=list(map(int,input().split())) EDGE=[list(map(int,input().split())) for i in range(M)] OK=0 NG=10**9+1 while NG>OK+1: mid=(OK+NG)//2 E=[[] for i in range(N)] for x,y,h in EDGE: if h>=mid: E[x-1].append(y-1) E[y-1].append(x-1) USE=[0]*N USE[0]=1 Q=[0] while Q: x=Q.pop() USE[x]=1 for to in E[x]: if USE[to]==0: USE[to]=1 Q.append(to) if USE[N-1]==1: OK=mid else: NG=mid if OK==0: print("NaN") else: print(OK)