#include #include using namespace std; using namespace atcoder; using ll=long long; using ld=long double; ld pie=3.141592653589793; ll inf=14449999999999994; ll mod=1000000007; int main(){ ll n; cin >> n; vector>g(n); for (ll i = 0; i < n-1; i++) { ll a,b; cin >> a >> b;a--,b--; g[a].push_back(b); g[b].push_back(a); } vectormemo(n,inf); memo[0]=0; queueque; que.push(0); while (!que.empty()) { ll v=que.front(); que.pop(); for (ll i = 0; i < g[v].size(); i++) { if (memo[g[v][i]]>memo[v]+1) { memo[g[v][i]]=memo[v]+1; que.push(g[v][i]); } } } ll id=0,x=0; for (ll i = 0; i < n; i++) { if (memo[i]>x) { id=i; x=memo[i]; } } que.push(id); for (ll i = 0; i < n; i++) { memo[i]=inf; } memo[id]=0; while (!que.empty()) { ll v=que.front(); que.pop(); for (ll i = 0; i < g[v].size(); i++) { if (memo[g[v][i]]>memo[v]+1) { memo[g[v][i]]=memo[v]+1; que.push(g[v][i]); } } } x=0; for (ll i = 0; i < n ; i++) { x=max(x,memo[i]); } cout << 2*(n-1)-x << endl; }