import sys from collections import deque, defaultdict input = sys.stdin.readline n, m = map(int, input().split()) std = [list(map(int, input().split())) for _ in range(m)] G = [defaultdict(int) for _ in range(n)] D = [] W = [float('inf')] * (m+2) for i in range(m): s, t, d = std[i] s -= 1 t -= 1 if G[s][t] < d: G[s][t] = d if G[t][s] < d: G[t][s] = d D.append(d) def bfs(cost): dist = [float('inf')] * n dist[0] = 0 q = deque([0]) while q: now = q.popleft() for nxt, d in G[now].items(): if dist[nxt] < dist[now] + 1: continue if d < cost: continue dist[nxt] = dist[now] + 1 if nxt == n-1: return dist[nxt] q.append(nxt) return float('inf') D.append(0) D.append(float('inf')) D.sort() ok = 0 ng = len(D) while ng - ok > 1: mid = (ng + ok) // 2 W[mid] = bfs(D[mid]) if W[mid] != float('inf'): ok = mid else: ng = mid print(D[ok], W[ok])