#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define mid(l, r) ((l)+(r))/2
#define len(a) (a).length()
#define sz(a) (a).size()
#define xx first
#define yy second
#define inf int(2e9)
#define ff(i, a, b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for(int (i) = (a); (i) >= (b); --(i))
#define  maxn 200005

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

template<class T>
void print(const T niz[], const int siz)
{
    for(int i=0;i<siz;i++)
        cout << niz[i] << " ";
    cout << endl;
}

vector<int> graf[maxn];
int n;
int dija, dijb;
int maks = 0;
int koji = 0;
int duz = 0;
void dfs(int x, int par, int dist){
    if(dist >= maks){
        maks = dist;
        koji = x;
    }
    for(auto c:graf[x]){
        if(c == par)continue;
        dfs(c, x, dist + 1);
    }
}
int najdalji(int x){
    maks = 0;
    koji = 0;
    dfs(x,0,0);
    return koji;
}
int dist[maxn];
void dfs2(int x, int par, int ud){
    dist[x] = ud;
    for(auto c:graf[x]){
        if(c == par)continue;
        dfs2(c, x, ud + 1);
    }
}
vector<int>put;
void nadji(int x){
    put.pb(x);
    if(x == dija)return;
    for(auto c:graf[x]){
        if(dist[c] < dist[x]){
            nadji(c);
            return;
        }
    }
}
int res = 1e9;
void uradi(int x){
    if(n == 2){
        res = 0;
        return;
    }
    if(n == 3){
        res = 1;
        return;
    }
    if(n == 4){
        res = 2;
        return;
    }
    //cout << endl;
    //cout << x << endl;
    dfs2(x, 0, 0);
    maks = 0;
    int dmaks = 0;
    ff(i,1,n){
        if(dist[i] > maks){
            dmaks = maks;
            maks = dist[i];
        }
        else if(dist[i] > dmaks){
            dmaks = dist[i];
        }
        //maks = max(maks, dist[i]);
    }
    int kol = 0;
    ff(i,1,n){
        if(dist[i] == maks)kol++;
    }
    if(n <= 4){

    }
    if(kol >= 2){
        res = min(res, 2*maks);
    }
    else{
        sort(dist + 1, dist + n + 1);
        if(n%2 == 1){
            res = min(res, dist[n - 2] + dist[n - 3]);
        }
        else res = min(res, dist[n - 1] + dist[n - 2]);
    }

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    ff(i,1,n - 1){
        int a,b;
        cin >> a >> b;
        graf[a].pb(b);
        graf[b].pb(a);
    }
    dija = najdalji(1);
    dijb = najdalji(dija);
    duz = maks;
    dfs2(dija, 0, 0);
    nadji(dijb);
    int vel = put.size();
    //for(auto c:put)cout << c<< " ";
    //cout << endl;
    int pola = vel/2;
    int gore = min(pola + 20, vel - 1);
    int dole = max(pola - 20, 0);
    ff(i,dole,gore){
        uradi(put[i]);
    }
    cout << res;
    return 0;
}