n,m = map(int,input().split()) graph = [set() for _ in range(n+1)] for _ in range(m): s,t,d = map(int,input().split()) graph[s].add((t,d)) graph[t].add((s,d)) from collections import deque def bfs(G,start,w): Deq = deque() dist = [-1] * (n + 1) Deq.append(start) dist[start] = 0 while Deq : pos = Deq.popleft() for nex,d in G[pos] : if dist[nex] == -1 and w <= d: dist[nex] = dist[pos] + 1 Deq.append(nex) return dist OK = 0 NG = 10**18 while NG - OK >= 2: MID = (OK+NG)//2 dist_MID = bfs(graph,1,MID) if dist_MID[n] != -1: OK = MID else: NG = MID dist_OK = bfs(graph,1,OK) print(OK,dist_OK[n])