import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Queue; import java.util.Scanner; public class No1473 { public static int n; public static ArrayList[] tdLists; public static void main(String[] args) throws IOException { // String[] readStrings = readStr(); Scanner scanner = new Scanner(System.in); // n = Integer.parseInt(readStrings[0].split(" ")[0]); // int m = Integer.parseInt(readStrings[0].split(" ")[1]); n = scanner.nextInt(); int m = scanner.nextInt(); tdLists = new ArrayList[m+1]; int[] dlist = new int[m]; int s , t , d; for(int i = 1;i <= m;i++) { /* s = Integer.parseInt(readStrings[i].split(" ")[0]); t = Integer.parseInt(readStrings[i].split(" ")[1]); d = Integer.parseInt(readStrings[i].split(" ")[2]); */ s = scanner.nextInt(); t = scanner.nextInt(); d = scanner.nextInt(); if(tdLists[s] == null) { tdLists[s] = new ArrayList(); } if(tdLists[t] == null ) { tdLists[t] = new ArrayList(); } tdLists[s].add(new tdMap(t, d)); tdLists[t].add(new tdMap(s, d)); dlist[i-1] = d; } scanner.close(); Arrays.sort(dlist); int a = 0 , b = dlist.length-1 , h = calchalf(a, b) , ans = 0; while(h != a) { ans = rootcount(dlist[h]); if(ans == 0) { b = h; h = calchalf(a, b); }else { a = h; h = calchalf(a, b); } } System.out.println(dlist[h] + " " + rootcount(dlist[h])); } public static int calchalf(int a , int b) { return (a+b)/2; } public static int rootcount(int w) { int[] root = new int[n+1]; Queue queue = new ArrayDeque(); ArrayList arrayList; queue.add(1); int s = 0; while(!queue.isEmpty()) { s = queue.poll(); arrayList = tdLists[s]; if(arrayList == null) { System.out.println("Empty"); continue; } for (tdMap tdMap : arrayList) { if(w <= tdMap.getd() && (root[tdMap.gett()] == 0 || root[tdMap.gett()] > root[s]+1)) { root[tdMap.gett()] = root[s] + 1; //System.out.println(tdMap.gett() + "-" + root[tdMap.gett()] + "-" + s + "-" + root[s]); queue.add(tdMap.gett()); } } } return root[n]; } public static String[] readStr() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList list = new ArrayList<>(); do { list.add(br.readLine()); }while(br.ready()); br.close(); String[] text = new String[list.size()]; list.toArray(text); return text; } public static class tdMap{ int t , d; public tdMap(int t , int d) { this.t = t; this.d = d; } public int getd() { return this.d; } public int gett() { return this.t; } } }