#include <bits/stdc++.h>
using namespace std;
int n;
vector <vector <int> > Graph;
int D;
bool Unique=true;
vector <bool> Bio;
#define top front
int Dist(int x,int y){
queue<pair<int,int> > q;

q.push({x,1});
while(q.size()){
    pair <int,int> t=q.top();
    q.pop();
    if(t.first==y)return t.second;
    if(Bio[t.first])continue;
    Bio[t.first]=true;
    for(auto nod:Graph[t.first]){
        q.push({nod,t.second+1});
    }
}

}


int FindFarest(int x){
int Group=0;
int Count=0;
queue<pair<int,int> > q;
q.push({x,0});
int farest=x;
while(q.size()){
    pair <int,int> t=q.top();
    int y=t.first;
    int cost=t.second;
    q.pop();
    if(Bio[y])continue;
    Bio[y]=true;
    farest=y;
    if(cost>Group){
        Group=cost;
        Count=1;
    }
    else{
        Count++;
    }
    for(auto nod:Graph[y]){
        q.push({nod,cost+1});
    }
}
if(Count>1)Unique=0;
return farest;
}
void FindDiameter(){
    Bio=vector <bool> (n,0);
    int Far1=FindFarest(0);
    Bio=vector <bool> (n,0);
    int Far2=FindFarest(Far1);
    Bio=vector <bool> (n,0);

    D=Dist(Far1,Far2);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    Unique=1;
    cin >>n;
    Graph.resize(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin >>x>>y;
        x--;
        y--;
        Graph[x].push_back(y);
        Graph[y].push_back(x);
    }
    FindDiameter();

   // cout <<(int)Unique<<" "<<D<<endl;
    if(n%2==0&&Unique==1)cout <<D-3<<endl;
    else if(Unique==1)cout <<D-2<<endl;
    else cout <<D-1<<endl;
}