#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(), x.end() #define int long long typedef priority_queue<array<int, 2>, vector<array<int,2>>, greater<>> prque; vector<vector<int>> g; vector<pair<int, int>> tinout; int tt = 0; vector<int> dst; vector<int> dst2, dst3; vector<int> pap; void dfst(int v) { tinout[v].first = tt++; for (auto i : g[v]) { if (v == pap[i]) dfst(i); } tinout[v].second = tt++; } bool ispapa(int papa, int son) { return tinout[papa].first <= tinout[son].first and tinout[son].second <= tinout[papa].second; } void bfsd(int v) { dst.assign(g.size(), -1); queue<pair<int, int>> que; que.push({v, 0}); while (!que.empty()) { auto [t, prc] = que.front(); que.pop(); if (dst[t] != -1) { continue; } dst[t] = prc; if (dst[t] == 0) pap[t] = t; for (auto i : g[t]) { if (dst[i] != -1 and dst[i] + 1 == dst[t]) { pap[t] = i; } if (dst[i] == -1) que.push({i, dst[t]+1}); } } } prque dfscalc(int v) { prque a; for (auto i : g[v]) { if (v == pap[i]) { prque eq = dfscalc(i); if (a.size() < eq.size()) { swap(a, eq); } while (!eq.empty()) { array<int, 2> tq = eq.top(); eq.pop(); a.push(tq); } } } while (!a.empty()) { array<int, 2> tq = a.top(); if (ispapa(v, tq[1])) { a.pop(); } else { break; } } for (auto i : g[v]) { if (!ispapa(v, i) and pap[v] != i) { a.push({dst[i] + 1 + dst[v], i}); } } if (!a.empty()) { dst2[v] = a.top()[0] - dst[v]; } return a; } void solve() { int n, m; cin >> n >> m; g.assign(n, {}); tinout.assign(n, {}); pap.assign(n, {}); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--;b--; g[a].push_back(b); g[b].push_back(a); } int fin = n-1; //cout << "T" << endl; bfsd(fin); //cout << "T" << endl; dfst(fin); //cout << "T" << endl; dst2.assign(n, -1); dfscalc(fin); dst3.assign(n, -1); //cout << "T" << endl; dst2[fin] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> quq; quq.push({0, fin}); //cout << "T" << endl; while (!quq.empty()) { auto [pr, v] = quq.top(); quq.pop(); if (dst3[v] != -1) continue; dst3[v] = pr; for (auto i : g[v]) { if (dst2[i] == -1) continue; quq.push({max(dst2[i], pr + 1), i}); } } //for (int i = 0; i < n; ++i) { // cout << dst[i] << ' ' << dst2[i] << ' ' << dst3[i] << '\n'; //} cout << dst3[0] << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); /* int x = 4e12; int c = 2025; int mx = 0; for (int i = 0; i < 1000000; ++i) { if ((mx = max(mx, solve(x - i * 1345239))) > c) { cout << solve(x - i * 1345239) << '\n'; exit(1); } cerr << mx << '\n'; } int prc = 0; */ int t = 1; //cin >> t; while (t--) solve(); }