from collections import deque n,m = map(int,input().split()); INF = float("inf") G = [[] for _ in range(n)] for i in range(m): s,t,d = map(int,input().split()) s-=1;t-=1 G[s].append((t,d)) G[t].append((s,d)) def BFS(w): D = [INF]*n D[0] = 0 Q = deque([]) Q.append(0) while Q: v = Q.popleft() for u,d in G[v]: if d < w: continue if D[u] <= D[v] + 1: continue D[u] = D[v] + 1 Q.append(u) return D ok = 1 ng = pow(10,9) + 1 while abs(ok-ng) > 1: mid = (ok+ng)//2 dis = BFS(mid) #print(dis,mid) if dis[n-1] < INF: ok = mid else: ng = mid dis = BFS(ok) ans = [ok,dis[n-1]]