#include <fstream>
using namespace std;
ifstream cin("stroll.in");
ofstream cout("stroll.out");
int a[1001],l1[1001],l2[1001],p,ans=0;
void dfs(int step,int x,int y)
{
int r;
if (x==0)
{
if (step>ans) ans=step;
}else
{
for (r=1;r<=p;r++) if (a[r]==x) {dfs(step+1,l1[r],l2[r]);break;}
}
if (y==0)
{
if (step>ans) ans=step;
}else
{
for (r=1;r<=p;r++) if (a[r]==y) {dfs(step+1,l1[r],l2[r]);break;}
}
}
int main()
{
int i,k;
cin>>p;
for (i=1;i<=p;i++)
{
cin>>a[i]>>l1[i]>>l2[i];
if (a[i]==1) k=i;
}
dfs(1,l1[k],l2[k]);
cout<<ans<<endl;
return 0;
}