#include <bits/stdc++.h>

using namespace std;

#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

int n;

vb pos(200000, 0);

void init1(){
    for (int i = 0; i < 110000; ++i){
        pos[i] = 0;
    }
}

int cnt, cnt1;

vi e[200000];
vi d1(200000, 0), d2(200000, 0);
vb vis(200000, 0);

void dfs(int x, int dij, int dis = 0){
    pos[x] = 1;

    if (dis == dij - 1)
        ++cnt;

    for (auto& i : e[x]){
        if (!pos[i]){
            dfs(i, dij, dis + 1);
        }
    }
}

queue<int> q;

void init(){

    for (int i = 0; i < 110000; ++i){
        vis[i] = 0;
        d2[i] = 0;
    }
}

set<pi> di;

signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    

    cin >> n;

    for (int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;

        e[u].push_back(v);
        e[v].push_back(u);
    }

    q.push(1);
    d1[1] = 0;
    vis[1] = 1;

    pi maxi = {1, 0};

    while(!q.empty()){
        int c = q.front();
        q.pop();

        for (auto& i : e[c]){
            if (vis[i]) continue;

            vis[i] = 1;
            d1[i] = d1[c] + 1;

            if (d1[i] > maxi.second){
                maxi.second = d1[i];
                maxi.first = i;
            }

            q.push(i);
        }
    }

    init();    
    vi idea;
    for (int i = 1; i <= n; ++i){
        if (d1[i] == maxi.second){
            idea.push_back(i);
        }
    }

    int len = idea.size(), zz = min(len, 4ll), dijametar = 0;
    for (int i = 0; i < zz; ++i){
        init();

        q.push(idea[i]);
        vis[idea[i]] = 1;
        d2[idea[i]] = 0;
        maxi = {idea[i], 0};

        while(!q.empty()){
            int c = q.front();
            q.pop();

            for (auto& j : e[c]){
                if (vis[j]) continue;

                vis[j] = 1;
                d2[j] = d2[c] + 1;

                if (d2[j] > maxi.second){
                    maxi.second = d2[j];
                    maxi.first = j;
                }

                q.push(j);
            }
        }

        dijametar = maxi.second;
        for (int j = 1; j <= n; ++j){
            if (d2[j] == maxi.second){
                int v = min(idea[i], j);
                di.insert({v, idea[i] + j - v});
            }
        }
    }

    int sz = di.size();
    if (sz > 3){
        return cout << dijametar << '\n', 0;
    }

    if (sz == 3){
        if (n % 2 == 0)
            return cout << dijametar - 1 << '\n', 0;
        else
            return cout << dijametar << '\n', 0;
    }

    if (sz == 1){
        int v = (*di.begin()).first, u = (*di.begin()).second;


        init1();
        dfs(v, dijametar, 0);
        cnt1 = cnt;
        init1();
        dfs(u, dijametar, 0);
        if (n % 2 || cnt > 1 || cnt1 > 1)
            return cout << dijametar - 2 << '\n', 0;
        else
            return cout << dijametar - 1 << '\n', 0;
    }

    if (sz == 2){
        if (n % 2 == 0)
            return cout << dijametar - 1 << '\n', 0;
        else
            return cout << dijametar << '\n', 0;
    }
}