N,M=map(int,input().split()) G=[[] for i in range(N)] for i in range(M): a,b,c=map(int,input().split()) G[a-1].append((b-1,c)) G[b-1].append((a-1,c)) l=0 r=10**9 while True: if l==r: break m=(l+r+1)//2 from collections import deque S=deque() d=[0]*N d[0]=1 S.append(0) while S: x=S.pop() for B in G[x]: y,c=B[:] if c>=m: if d[y]==0: d[y]=1 S.append(y) if d[N-1]==1: l=m else: r=m-1 if l==0: print('NaN') else: print(l)