#include<iostream>
#include<cstring>
using namespace std;
#define MAX 1000
int lt[MAX];
int rt[MAX];
void built(int i){
cin>>lt[i];
cin>>rt[i];
}
void dfs(int num,int &deepth,int &max){
deepth++;
if(lt[num]==0&&rt[num]==0){
if(deepth>max){
max=deepth;
}
deepth--;
return ;
}
if(lt[num]!=0){
dfs(lt[num],deepth,max);
}
if(rt[num]!=0){
dfs(rt[num],deepth,max);
}
deepth--;
return;
}
int main(){
memset(lt,0,sizeof(lt));
memset(rt,0,sizeof(rt));
int n,i=1;
cin>>n;
int deepth=0;
int max=0;
while(n--){
built(i);
i++;
}
dfs(1,deepth,max);
cout<<max;
return 0;
}