# Python 二分探索解 AC になってほしい from collections import deque import sys input = sys.stdin.readline def main(): N, M = map(int, input().split()) to = [[] for _ in range(N)] ds = [] for _ in range(M): s, t, d = map(int, input().split()) s -= 1 t -= 1 to[s].append((t, d)) to[t].append((s, d)) ds.append(d) ds = sorted(set(ds)) ok, ng = 0, len(ds) def calc(th: int): dist = [1 << 30] * N dist[0] = 0 q = deque([0]) while q: now = q.popleft() for nxt, d in to[now]: if d >= th and dist[nxt] > dist[now] + 1: dist[nxt] = dist[now] + 1 q.append(nxt) return dist[-1] while ng - ok > 1: c = (ok + ng) // 2 x = calc(ds[c]) if x < 1 << 30: ok = c else: ng = c print(ds[ok], calc(ds[ok])) if __name__ == '__main__': main()