import java.io.PrintWriter import java.util.* fun PrintWriter.solve() { val n = nextInt() val m = nextInt() val data = mutableListOf>() val adj = Array(n) { mutableListOf() } val uf = UnionFind(n) for (i in 0 until m) { val s = nextInt() - 1 val t = nextInt() - 1 val d = nextInt() data.add(Triple(s, t, d)) } data.sortByDescending { it.third } for ((s, t, d) in data) { uf.unite(s, t) if (uf.same(0, n - 1)) { for ((s2, t2, d2) in data) { if (d2 >= d) { adj[s2].add(t2) adj[t2].add(s2) } } val que: Queue> = ArrayDeque() que.add(0 to 0) val visited = BooleanArray(n) { false } visited[0] = true while (que.isNotEmpty()) { val (v, num) = que.poll() if (v == n - 1) { println("$d $num") return } for (w in adj[v]) { if (!visited[w]) { que.add(w to num + 1) visited[w] = true } } } } } } class UnionFind(n: Int) { private val rank = IntArray(n) { 0 } private val parent = IntArray(n) { -1 } fun find(x: Int): Int { if (parent[x] < 0) { return x } parent[x] = find(parent[x]) return parent[x] } fun unite(x: Int, y: Int) { val x1 = find(x) val y1 = find(y) if (x1 == y1) { return } if (rank[x1] < rank[y1]) { parent[y1] += parent[x1] parent[x1] = y1 } else { parent[x1] += parent[y1] parent[y1] = x1 if (rank[x1] == rank[y1]) { rank[x1]++ } } } fun same(x: Int, y: Int): Boolean { return find(x) == find(y) } fun size(x: Int): Int { return -parent[find(x)] } } fun main() { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() } // region Scanner private var st = StringTokenizer("") private val br = System.`in`.bufferedReader() fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() // endregion