import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Queue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int m = Integer.parseInt(sa[1]); Hen[] arr = new Hen[m]; for (int i = 0; i < m; i++) { sa = br.readLine().split(" "); Hen h = new Hen(); h.s = Integer.parseInt(sa[0]) - 1; h.t = Integer.parseInt(sa[1]) - 1; h.d = Integer.parseInt(sa[2]); arr[i] = h; } br.close(); Arrays.sort(arr, (o1, o2) -> o2.d - o1.d); UnionFind uf = new UnionFind(n); List> list = new ArrayList<>(); int w = 0; for (int i = 0; i < n; i++) { list.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { Hen h = arr[i]; uf.union(h.s, h.t); list.get(h.s).add(h.t); list.get(h.t).add(h.s); if (uf.num == 1) { w = h.d; break; } } Queue que = new ArrayDeque<>(); que.add(0); int[] d = new int[n]; d[0] = 1; while (!que.isEmpty()) { int cur = que.poll(); for (int i : list.get(cur)) { if (d[i] == 0) { que.add(i); d[i] = d[cur] + 1; } } } System.out.println(w + " " + (d[n - 1] - 1)); } static class UnionFind { int[] parent, size; int num = 0; // 連結成分の数 UnionFind(int n) { parent = new int[n]; size = new int[n]; num = n; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } void union(int x, int y) { int px = find(x); int py = find(y); if (px != py) { parent[px] = py; size[py] += size[px]; num--; } } int find(int x) { if (parent[x] == x) { return x; } parent[x] = find(parent[x]); return parent[x]; } /** * xとyが同一連結成分か */ boolean same(int x, int y) { return find(x) == find(y); } /** * xを含む連結成分のサイズ */ int size(int x) { return size[find(x)]; } } static class Hen { int s, t, d; } }