from collections import deque def check(x): dist = [-1]*n dist[0] = 0 q = deque([0]) while q: v = q.popleft() for b,w in g[v]: if dist[b] == -1 and x <= w: dist[b] = dist[v] + 1 q.append(b) if dist[-1] == -1: return False,-1 else: return True, dist[-1] n,m = map(int,input().split()) g = [[] for _ in range(n)] for _ in range(m): a,b,w = map(int,input().split()) g[a-1].append((b-1,w)) g[b-1].append((a-1,w)) ok = 0 ng = 10**9+1 val = -1 while ng-ok > 1: mid = (ok+ng)//2 tf,v = check(mid) if tf: ok = mid val = v else: ng = mid print(ok,val)