#include<bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple

using namespace std;
const int N = 1e5;

vector<int> graf[N + 10];
bool mark[N + 10];
int dist[N + 10];
int dubina[N + 10];
int potomci[N + 10];
int maxDist = 0;
int maxInd = 0;
int maxDubina = 0;

void dfs(int x)
{
    mark[x] = true;
    for(int i = 0; i < graf[x].size(); i++)
    {
        int cvor = graf[x][i];
        if(!mark[cvor])
        {
            dist[cvor] = dist[x] + 1;
            if(maxDist < dist[cvor])
            {
                maxDist = dist[cvor];
                maxInd = cvor;
            }
            dfs(cvor);
        }
    }
}

bool nadji(int x, int goal)
{
    mark[x] = true;
    if(x == goal) return true;
    for(int i = 0; i < graf[x].size(); i++)
    {
        int cvor = graf[x][i];
        dist[cvor] = dist[x] + 1;
        if(!mark[cvor])
            if(nadji(cvor, goal))
                return true;
    }
    return false;
}

int loose(int x)
{
    mark[x] = true;
    for(int i = 0; i < graf[x].size(); i++)
    {
        int cvor = graf[x][i];
        dist[cvor] = dist[x] + 1;
        if(!mark[cvor])
        {
            dubina[x] = max(dubina[x], loose(cvor) + 1);
            maxDubina = max(maxDubina, dubina[x]);
            potomci[x]++;
        }
    }
    return dubina[x];
}

int koliko(int x, int goalDubina)
{
    mark[x] = true;
    int s = 0;
    for(int i = 0; i < graf[x].size(); i++)
    {
        int cvor = graf[x][i];
        if(!mark[cvor])
        {
            s += koliko(cvor, goalDubina);
        }
    }
    mark[x] = false;
    if(goalDubina == dist[x] && potomci[x] == 0) s++;
    return s;
}

void pred()
{
    memset(mark, false, sizeof(mark));
    memset(dist, 0, sizeof(dist));
    maxDist = 0;
    maxInd = 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);


    int n;
    cin >> n;
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        graf[a].pb(b);
        graf[b].pb(a);
    }
    dist[0] = 0;
    dfs(0);

    int cvor1 = maxInd;
    pred();

    dfs(cvor1);
    int cvor2 = maxInd;
    int bestDist = maxDist;

    int k = dist[cvor2] / 2;
    //cout << cvor1 << " " << cvor2;
    int koren = 0;
    for(int i = 0; i < n; i++)
        if(dist[i] == k) {koren = i; break;}

    pred();

    loose(koren);
    pred();

    maxDubina--;
    if(maxDubina == 0)
    {
        if(potomci[koren] == 2) cout << 1;
        else cout << 2;
        return 0;
    }

    pred();
    dfs(koren);

    maxDubina++;

    int najdublji = maxDubina;

    maxDubina--;

    int cnt = 0;
    int p1 = 0;
    int p2 = 0;

    memset(mark, 0, sizeof(mark));
    mark[koren] = true;

    //for(int i = 0; i < n; i++) cout << i << " " << dist[i] << "\n";
    int mul1 = 0;
    int mul2 = 0;
    for(int i = 0; i < graf[koren].size(); i++)
    {
        int cvor = graf[koren][i];
        if(dubina[cvor] == maxDubina)
        {
            cnt++;
            int r = koliko(cvor, najdublji);
            if(r > 0) p1++;
            if(r > 1) mul1++;
        }
        if(dubina[cvor] == maxDubina - 1)
        {
            //cout << cvor << "** ";
            int r = koliko(cvor, najdublji - 1);
            if(r > 0) p2++;
            if(r > 1) mul2++;
        }
    }
    int kol = 0;
    //cout << koren << "\n";
    //cout << p1 << " " << p2 << " " << mul1 << " " << mul2 << " ";
    //cout << cnt << " ";

    if(cnt == 1)
    {
        if((p1 > 1 || mul1 >= 1) && (p2 > 1 || mul2 >= 1)) kol = 2;
        else if((p1 > 1 || mul1 >= 1) || (p2 > 1 || mul2 >= 1)) kol = 1;
        else kol = 0;
    }
    else
    {
        if(p1 == 2)
        {
            if(mul1 == 0) kol = 0;
            if(mul1 == 1) kol = 1;
            if(mul1 > 1) kol = 2;
        }
        if(p1 == 3)
        {
            if(mul1 == 0) kol = 1;
            else kol = 2;
        }
        if(p1 > 3) kol = 2;
    }

    if(kol > 1) cout << bestDist;
    if(kol == 0)
    {
        if(n % 2 == 0) cout << bestDist-2; // (n-2)%2
        else cout << bestDist - 1;
    }
    if(kol == 1)
    {
        if((n-1) % 2 == 0) cout << bestDist; // (n-3)%2
        else cout << bestDist - 1;
    }
    return 0;
}