#include <bits/stdc++.h>
#define MAXN 100005
using namespace std;

bool vis[MAXN];
vector<int> v[MAXN];
int l[MAXN];
int parent[MAXN];

vector<int> bfs1(int node)
{
    vector<int> t;

    t.push_back(node);

    vis[node]=true;

    l[node]=0;

    queue<int> q;

    q.push(node);

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

        for(int i=0;i<v[x].size();i++)
        {
            if(!vis[ v[x][i] ])
            {
                t.push_back( v[x][i] );

                q.push( v[x][i] );

                l[ v[x][i] ]=l[x]+1;

                vis[ v[x][i] ]=true;
            }
        }
    }

    return t;
}


void bfs(int node)
{
    vis[node]=true;

    l[node]=0;

    parent[node]=-1;

    queue<int> q;

    q.push(node);

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

        for(int i=0;i<v[x].size();i++)
        {
            if(!vis[ v[x][i] ])
            {
                q.push( v[x][i] );

                l[ v[x][i] ]=l[x]+1;

                parent[ v[x][i] ]=x;

                vis[ v[x][i] ]=true;
            }
        }
    }
}

void vrati(int n)
{
    for(int i=0;i<=n;i++) vis[i]=false;

    for(int i=0;i<=n;i++) l[i]=0;
}

int nadji(int n)
{
    int md=0,p;

    for(int i=1;i<=n;i++)
    {
        if(md<l[i])
        {
            md=l[i];

            p=i;
        }
    }

    return p;
}

int main()
{ int n,a,b,i,md,diam,p1,p2,f,uk=0;

bool ok3=false,ok2=false;

scanf("%d",&n);

for(i=1;i<n;i++)
{
    scanf("%d%d",&a,&b);

    v[a].push_back(b);

    v[b].push_back(a);
}

bfs(1);

p1=nadji(n);

vrati(n);

bfs(p1);

p2=nadji(n);

diam=l[p2];

int cnt1=0;

for(i=1;i<=n;i++)
{
    if(l[i]==diam) cnt1++;
}

vrati(n);

bfs(p2);

int cnt2=0;

for(i=1;i<=n;i++)
{
    if(l[i]==diam) cnt2++;
}

//cout<<diam;

int br[3],af=0;

if(diam%2==0)
{
    int tmp=diam/2;

    f=p1;

    for(i=1;i<=tmp;i++) f=parent[f];

    vrati(n);

    vis[f]=true;

    for(i=0;i<v[f].size();i++)
    {
        vector<int> tp=bfs1( v[f][i] );

        int jm=0;

        for(int j=0;j<tp.size();j++)
        {
            if(l[tp[j]]==(tmp-1)) jm++;
        }

        tp.erase(tp.begin(),tp.end());

        if(jm==1) { uk++; br[1]++; }

        if(jm>1) { uk++; br[2]++; af+=jm; }
    }

    if(uk>=3 || ( uk==2 && br[2]>=1) ) ok3=true;
}

if( max(cnt1,cnt2) >=2 ) ok2=true;

int sol;

if(ok3==true)
{
    if(uk>=4) sol=diam;
    else
    {

        int dif=n-(br[1]+af);

        if(uk==3)
        {
            if(br[1]==3 && (dif%2)==0) sol=diam;

            if(br[1]==3 && (dif%2)==1) sol=diam-1;

            if(br[1]==2) sol=diam;

            if(br[1]==1) sol=diam;

            if(br[1]==0) sol=diam;
        }

        if(uk==2)
        {
            if(br[1]==1 && (af%2)==0 && (dif%2)==0) sol=diam;

            if(br[1]==1 && (af%2)==0 && (dif%2)==1) sol=diam-1;

            if(br[1]==1 && (af%2)==1 && (dif%2)==0) sol=diam-1;

            if(br[1]==1 && (af%2)==1 && (dif%2)==1) sol=diam;

            if(br[1]==0) sol=diam;
        }
    }
}

if(ok3==false && ok2==true ) sol=diam-1;

if(ok3==false && ok2==false)
{
    if(n%2==1) sol=diam-1;

    if(n%2==0) sol=diam-2;
}

printf("%d\n",sol);
    return 0;
}