import sys input = sys.stdin.readline from collections import deque n,m=map(int,input().split()) E=[[] for i in range(n+1)] for i in range(m): s,t,d=map(int,input().split()) E[s].append((t,d)) E[t].append((s,d)) OK=1 NG=1<<31 while NG-OK>1: mid=(OK+NG)//2 GO=[0]*(n+1) GO[1]=1 Q=[1] while Q: x=Q.pop() for to,d in E[x]: if d>=mid and GO[to]==0: GO[to]=1 Q.append(to) if GO[n]==1: OK=mid else: NG=mid X=[-1]*(n+1) X[1]=0 Q=deque([1]) while Q: x=Q.popleft() for to,d in E[x]: if d>=OK and X[to]==-1: X[to]=X[x]+1 Q.append(to) print(OK,X[n])