from collections import deque N,M = map(int,input().split()) G = {i:[] for i in range(1,N+1)} for _ in range(M): s,t,d = map(int,input().split()) G[s].append((t,d)) G[t].append((s,d)) high = 10**9+10 low = 0 ans = -1 while high-low>1: mid = (high+low)//2 dist = [-1]*(N+1) dist[1] = 0 que = deque([1]) while que: x = que.popleft() for y,d in G[x]: if dist[y]==-1 and mid<=d: dist[y] = dist[x]+1 que.append(y) if dist[N]>=0: ans = dist[N] low = mid else: high = mid print(low,ans)