#include<bits/stdc++.h>
using namespace std;
struct nood{
int l,r;
};
struct nood2{
int x,num;
};
nood a[1000005];
int n,l,r,ans;
queue<nood2> q; nood2 temp;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&l,&r);
a[i].l = l;
a[i].r = r;
}
q.push({1,1});
while(!q.empty()){
temp = q.front();
q.pop();
ans = max(ans,temp.num);
if(a[temp.num].l!=0)q.push({a[temp.num].l , temp.num+1});
if(a[temp.num].r!=0)q.push({a[temp.num].r , temp.num+1});
}
cout<<ans;
return 0;
}
?