package yukicoder; import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Arrays; public class No1473_3 { private static class Q { int i; int d; public Q(int i, int d) { this.i = i; this.d = d; } } private static int n; private static List> V; public static void main(String[] args) { Scanner scan = new Scanner(System.in); n = scan.nextInt(); int m = scan.nextInt(); V = new ArrayList>(); for (int i = 0; i < n; i++) { V.add(new ArrayList()); } int[] w = new int[m+2]; int max = 0; for (int i=0; i < m; i++) { int s = scan.nextInt() - 1; int t = scan.nextInt() - 1; int d = scan.nextInt(); V.get(s).add(new Q(t, d)); V.get(t).add(new Q(s, d)); w[i+1] = d; max = Math.max(max, d); } w[m+1] = max; Arrays.sort(w); scan.close(); for (int i=0; i < n; i++) { V.get(i).sort((a,b) -> b.d - a.d); } Q l = new Q(0, 0), r = new Q(m+1, 0); while (r.i - l.i > 1) { int mid = (r.i + l.i) / 2; int c = bfs(w[mid]); if (c < 0) { r.i = mid; r.d = 0; } else { l.i = mid; l.d = c; } } System.out.println(String.format("%d %d", w[l.i],l.d)); } private static int bfs(int w) { int[] dist = new int[n]; Arrays.fill(dist, -1); List que = new ArrayList(); dist[0] = 0; que.add(0); while (!que.isEmpty()) { int v = que.remove(0); for (Q nv : V.get(v)) { if (nv.d >= w) { if (dist[nv.i] < 0) { dist[nv.i] = dist[v] + 1; if (nv.i == n - 1) { return dist[nv.i]; } que.add(nv.i); } } } } return dist[n-1]; } }