def main(): from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! ''' ''' N,M = map(int, input().split()) G = [[] for _ in range(N+1)] w = [] for _ in range(M): a,b,c = map(int, input().split()) G[a].append((b,c)) G[b].append((a,c)) w.append(c) w.sort() r = M-1 l = 0 while r-l > 1: m = (r+l)//2 Q = deque() Q.append((1,0)) seen = [0]*(N+1) ans = -1 while len(Q) > 0: pos,cnt = Q.popleft() if seen[pos] == 1: continue seen[pos] = 1 if pos == N: ans = cnt break for nx,d in G[pos]: if d >= w[m] and seen[nx] == 0: Q.append((nx,cnt+1)) if ans == -1: l = m else: r = m Q = deque() Q.append((1,0)) seen = [0]*(N+1) ans = -1 while len(Q) > 0: pos,cnt = Q.popleft() if seen[pos] == 1: continue seen[pos] = 1 if pos == N: ans = cnt break for nx,d in G[pos]: if d >= w[r] and seen[nx] == 0: Q.append((nx,cnt+1)) print(w[r],ans) if __name__ == '__main__': main()