from collections import deque n, m = map(int, input().split()) G = [[] for i in range(n)] for _ in range(m): s, t, d = map(int, input().split()) s -= 1; t -= 1 G[s].append((t, d)) G[t].append((s, d)) INF = 10 ** 18 def is_ok(x): dist = [INF] * n dist[0] = 0 Q = deque([0]) while Q: p = Q.popleft() dc = dist[p] + 1 for u, d in G[p]: if d >= x and dist[u] > dc: dist[u] = dc Q.append(u) return dist[-1] l, r = 0, 10 ** 9 + 1 while r - l > 1: mid = (l + r) // 2 tmp = is_ok(mid) if tmp == INF: r = mid else: l = mid print(l, is_ok(l))