package yukicoder; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class No1473_2 { private static class Q { int i; int d; public Q(int i, int d) { this.i = i; this.d = d; } public boolean equals(Object o) { if (o == this) return true; if (o.getClass() != this.getClass()) return false; Q q = (Q)o; return this.i == q.i; } } 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 max_d = 0; int min_d = Integer.MAX_VALUE; for (int i=0; i < m; i++) { int s = scan.nextInt() - 1; int t = scan.nextInt() - 1; int d = scan.nextInt(); Q q = new Q(t, d); int j = V.get(s).indexOf(q); if (j < 0) { V.get(s).add(q); } else { V.get(s).get(j).d = d; } q = new Q(s, d); j = V.get(t).indexOf(q); if (j < 0) { V.get(t).add(q); } else { V.get(t).get(j).d = d; } max_d = Math.max(max_d, d); min_d = Math.min(min_d, d); } scan.close(); for (int i=0; i < n; i++) { V.get(i).sort((a,b) -> b.d - a.d); } Q l = new Q(min_d - 1, 0), r = new Q(max_d + 1, 0); while (r.i - l.i > 1) { int mid = (r.i + l.i) / 2; int c = bfs(mid); if (c == 0) { r.i = mid; r.d = 0; } else { l.i = mid; l.d = c; } } System.out.println(String.format("%d %d", l.i,l.d)); } private static int bfs(int w) { boolean[]B = new boolean[n]; List fw = new ArrayList(); fw.add(0); int depth = 1; while (true) { List newfw = new ArrayList(); for (int i : fw) { B[i] = true; for (Q q : V.get(i)) { if (q.d >= w) { if (q.i == n - 1) { return depth; } if (!B[q.i]) { newfw.add(q.i); } } else { break; } } } if (newfw.isEmpty()) { depth = 0; break; } fw = newfw; depth++; } return depth; } }