class UnionFind: def __init__(self, n): self.par = [-1]*n self.rank = [0]*n def Find(self, x): if self.par[x] < 0: return x else: self.par[x] = self.Find(self.par[x]) return self.par[x] def Unite(self, x, y): x = self.Find(x) y = self.Find(y) if x != y: if self.rank[x] < self.rank[y]: self.par[y] += self.par[x] self.par[x] = y else: self.par[x] += self.par[y] self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def Same(self, x, y): return self.Find(x) == self.Find(y) def Size(self, x): return -self.par[self.Find(x)] import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n, m = map(int, input().split()) edge = [] for i in range(m): s, t, d = map(int, input().split()) s, t = s-1, t-1 edge.append((s, t, d)) edge.sort(key=lambda x: -x[2]) M = 10**18 uf = UnionFind(n) g = [[] for i in range(n)] for s, t, d in edge: if uf.Same(0, n-1) and d < M: continue else: uf.Unite(s, t) M = min(M, d) g[s].append(t) g[t].append(s) #print(M) from collections import deque q = deque([]) q.append(0) dist = [-1]*n dist[0] = 0 while q: v = q.popleft() for u in g[v]: if dist[u] == -1: q.append(u) dist[u] = dist[v]+ 1 print(M, dist[n-1])