
#include<iostream>
using namespace std;
struct node{
struct node*left1=NULL;
struct node*right1=NULL;
};
typedef struct node list;
void judge(node*a,int now,int*ans){
if(a->left1!=NULL){
now++;
*ans=max(now,*ans);
judge(a->left1,now,ans);
judge(a->right1,now,ans);
}
if(a->right1!=NULL){
if(a->left1!=NULL)now--;
now++;
*ans=max(now,*ans);
judge(a->left1,now,ans);
judge(a->right1,now,ans);
}
}
int main(){
int n;
cin>>n;
node a[n+1];
for(int i=1;i<=n;i++){
int p,q;
cin>>p>>q;
node*l=&a[p];
node*r=&a[q];
a[i].left1=l;
a[i].right1=r;
if(p==0)a[i].left1=NULL;
if(q==0)a[i].right1=NULL;
}
int ans=1;
judge(&a[1],1,&ans);
cout<<ans<<endl;
return 0;
}