""" """ import sys from sys import stdin def uf_find(n,p): ufl = [] while p[n] != n: ufl.append(n) n = p[n] for i in ufl: p[i] = n return n def uf_union(a,b,p,rank): ap = uf_find(a,p) bp = uf_find(b,p) if ap == bp: return True else: if rank[ap] > rank[bp]: p[bp] = ap elif rank[ap] < rank[bp]: p[ap] = bp else: p[bp] = ap rank[ap] += 1 return False from collections import deque def NC_Dij(lis,start): ret = [float("inf")] * len(lis) ret[start] = 0 q = deque([start]) plis = [i for i in range(len(lis))] while len(q) > 0: now = q.popleft() for nex in lis[now]: if ret[nex] > ret[now] + 1: ret[nex] = ret[now] + 1 plis[nex] = now q.append(nex) return ret,plis n,m = map(int,stdin.readline().split()) lis = [ [] for i in range(n)] duv = [] for i in range(m): u,v,d = map(int,stdin.readline().split()) u -= 1 v -= 1 duv.append( (d,u,v) ) duv.sort() duv.reverse() p = [i for i in range(n)] rank = [0] * n maxid = float("inf") for d,u,v in duv: if maxid == float("inf"): uf_union(u,v,p,rank) if uf_find(0,p) == uf_find(n-1,p): maxid = d lis[u].append(v) lis[v].append(u) elif maxid == d: lis[u].append(v) lis[v].append(u) dlis,tmp = NC_Dij(lis,0) print (maxid,dlis[-1])