mod = 1000000007 eps = 10**-9 def main(): import sys from collections import deque input = sys.stdin.buffer.readline class UnionFind(): def __init__(self, n): self.n = n self.root = [-1] * (n + 1) self.rnk = [0] * (n + 1) def find_root(self, x): while self.root[x] >= 0: x = self.root[x] return x def unite(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return elif self.rnk[x] > self.rnk[y]: self.root[x] += self.root[y] self.root[y] = x else: self.root[y] += self.root[x] self.root[x] = y if self.rnk[x] == self.rnk[y]: self.rnk[y] += 1 def isSameGroup(self, x, y): return self.find_root(x) == self.find_root(y) def size(self, x): return -self.root[self.find_root(x)] N, M = map(int, input().split()) E = [] for _ in range(M): E.append(tuple(map(int, input().split()))) E.sort(key=lambda x: x[2], reverse=True) UF = UnionFind(N) for a, b, c in E: UF.unite(a, b) if UF.isSameGroup(1, N): w = c break adj = [[] for _ in range(N+1)] for a, b, c in E: if c < w: break adj[a].append(b) adj[b].append(a) que = deque() que.append(1) seen = [-1] * (N+1) seen[1] = 0 par = [0] * (N+1) child = [[] for _ in range(N+1)] seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v child[v].append(u) que.append(u) seq.reverse() print(w, seen[N]) if __name__ == '__main__': main()