#include #define rep(i,n) for(int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair; using vv = vector>; const int INF = (int)1e9; const LL LINF = (LL)1e18; const int Max_N = (int)2e5; class UnionFind { public: vector par; vector siz; UnionFind(long long sz_): par(sz_), siz(sz_, (long long)1) { for (long long i = 0; i < sz_; ++i) par[i] = i; } void init(long long sz_) { par.resize(sz_); siz.assign(sz_, (long long)1); for (long long i = 0; i < sz_; ++i) par[i] = i; } long long root(long long x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool unite(long long x, long long y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool same(long long x, long long y) { return root(x) == root(y); } long long size(long long x) { return siz[root(x)]; } }; vv G; int depth = 0, f = 0; void dfs(int n, int d = 0, int par = -1){ if(d >= depth){ depth = d; f = n; } for(auto v : G[n]){ if(v == par) continue; dfs(v, d + 1, n); } } int main(){ int N; cin >> N; assert(2 <= N and N <= Max_N); UnionFind uf(N); G.resize(N); rep(i,N-1){ int a, b; cin >> a >> b; assert(1 <= a and a < b and b <= N); a--; b--; G[a].emplace_back(b); G[b].emplace_back(a); uf.unite(a, b); } int r = 0; rep(i,N) if(uf.root(i) == i) r++; assert(r == 1); dfs(0); dfs(f); cout << (N - 1) * 2 - depth << endl; return 0; }