local n, m = io.read("*n", "*n") local parent = {} local edge = {} local len = {} for i = 1, n do parent[i] = i edge[i] = {} len[i] = -1 end len[1] = 0 local function uf_findroot(idx) local idx_update = idx while parent[idx] ~= idx do idx = parent[idx] end while parent[idx_update] ~= idx do parent[idx_update], idx_update = idx, parent[idx_update] end return idx end local edgeinfo = {} for i = 1, m do edgeinfo[i] = {io.read("*n", "*n", "*n")} end table.sort(edgeinfo, function(a, b) return a[3] > b[3] end) local w = 0 for i = 1, m do local a, b = edgeinfo[i][1], edgeinfo[i][2] local c = edgeinfo[i][3] if c < w then break end table.insert(edge[a], b) table.insert(edge[b], a) local ra, rb = uf_findroot(a), uf_findroot(b) parent[rb], parent[b] = ra, ra local r1, rn = uf_findroot(1), uf_findroot(n) if r1 == rn then w = edgeinfo[i][3] end end local tasks = {1} local done = 0 while done < #tasks do done = done + 1 local src = tasks[done] for i = 1, #edge[src] do local dst = edge[src][i] if len[dst] < 0 then len[dst] = len[src] + 1 table.insert(tasks, dst) end end end print(w .. " " .. len[n])