# 体重何キロまで行ける、は単調なので二分探索可能 # ダイクストラでTLEしたのでBFSにする N, M = map(int, input().split()) edge_list = [] for i in range(M): s, t, d = map(int, input().split()) edge_list.append((s, t, d)) from collections import deque INF = 10**5+2 # TLEしたのでINF下げた def BFS(W): edges = [[] for i in range(N+1)] for s, t, d in edge_list: if d >= W: edges[s].append(t) edges[t].append(s) start = 1 goal = N distance = [INF]*(N+1) distance[start] = 0 que = deque() que.append(start) while que: current = que.popleft() for nxt in edges[current]: if distance[nxt] > distance[current]+1: distance[nxt] = distance[current]+1 que.append(nxt) if distance[N] == INF: return 0, INF else: return 1, distance[N] OK = 0 NG = 10**9+1 while NG-OK>1: mid = (NG+OK)//2 if BFS(mid)[0] == 1: OK = mid else: NG = mid ans_W = OK ans_distance = BFS(ans_W)[1] print(ans_W, ans_distance)