http://media.hihocoder.com/contests/challenge14/hihoCoder-Challenge14-Solution.pdf
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 120051
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=~0u>>1;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
//**************************************************************************************
vector<int> E[maxn];
int fa[maxn];
int dep[maxn];
int key[maxn];
void dfs(int x,int deep)
{
dep[x]=deep;
for(int i=0;i<E[x].size();i++)
{
int v=E[x][i];
if(key[v])
continue;
fa[v]=x;
dfs(v,deep+1);
}
}
int main()
{
int n=read();
for(int i=1;i<n;i++)
{
int a=read(),b=read();
E[a].push_back(b);
}
dfs(1,1);
int mx=0,bj;
for(int i=1;i<=n;i++)
{
if(dep[i]>mx)
{
mx=dep[i];
bj=i;
}
}//找到最长链
int ans=mx;
int x = bj;
while(x!=1)
{
key[x]=1;
x=fa[x];
}
key[1]=1;
for(int i=0;i<=n;i++)
{
if(key[i])
{
dfs(i,0);
}
}
mx = 0;
for(int i=1;i<=n;i++)
{
if(!key[i])
mx = max(mx,dep[i]);
}
ans = max(0,ans+mx-1);
printf("%d\n",ans);
return 0;
}