#include<cstdio>
#include<cstring>
using namespace std;
int n,i;
int d1[2000];
int d2[2000];
int dfs(int i){
int x;
int y;
if (i==0) return 0;
x=dfs(d1[i]);
y=dfs(d2[i]);
return x>y?x+1:y+1;
}
int main()
{
freopen("stroll.in","r",stdin);
freopen("stroll.out","w",stdout);
scanf("%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&i);
scanf("%d%d",&d1[i],&d2[i]);
}
printf("%d",dfs(1));
return 0;
}