n,m=map(int,input().split()) e=[[] for i in range(n)] for _ in range(m): u,v,d=map(int,input().split()) u-=1 v-=1 e[u]+=[(v,d)] e[v]+=[(u,d)] def check(w): v=[-1]*n q=[0] v[0]=0 for s in q: for t,d in e[s]: if v[t]==-1 and d>=w: v[t]=v[s]+1 q+=[t] return v[n-1] ok=0 ng=10**9+1 while ng-ok>1: m=(ok+ng)//2 if check(m)!=-1: ok=m else: ng=m print(ok,check(ok))