#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// /** * @brief https://tkmst201.github.io/Library/DataStructure/UnionFind.hpp */ struct UnionFind { using size_type = std::size_t; private: size_type n; std::vector dat; public: explicit UnionFind(size_type n) : n(n), dat(n, -1) {} size_type size(size_type x) noexcept { assert(x < n); return -dat[find(x)]; } size_type find(size_type x) noexcept { assert(x < n); if (dat[x] < 0) return x; return dat[x] = find(dat[x]); } void unite(size_type x, size_type y) noexcept { assert(x < n); assert(y < n); x = find(x); y = find(y); if (x == y) return; if (dat[x] < dat[y]) std::swap(x, y); dat[y] += dat[x]; dat[x] = y; } bool issame(size_type x, size_type y) noexcept { assert(x < n); assert(y < n); return find(x) == find(y); } }; int main() { int N, M; cin >> N >> M; vector> g(N); UnionFind uf(N); vector S(M), T(M), D(M); REP(i, M) scanf("%d %d %d", &S[i], &T[i], &D[i]), --S[i], --T[i]; vector ord(M); iota(ALL(ord), 0); sort(ALL(ord), [&](int i, int j) { return D[i] > D[j]; }); int mx = 0; for (int i : ord) { uf.unite(S[i], T[i]); g[S[i]].emplace_back(T[i]); g[T[i]].emplace_back(S[i]); mx = D[i]; if (uf.issame(0, N - 1)) break; } queue que; vector dist(N, INF); que.emplace(0); dist[0] = 0; while (!que.empty()) { auto u = que.front(); que.pop(); for (int v : g[u]) if (chmin(dist[v], dist[u] + 1)) que.emplace(v); } cout << mx << " " << dist[N - 1] << endl; }