#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
typedef int BTDataType;
typedef struct BTNode
{
BTDataType x;
struct BTNode *left;
struct BTNode *right;
} BTNode;
typedef BTNode *Tree;
Tree CreatTree(BTDataType x);
int main()
{
Tree T = CreatTree(1);
Tree temp;
Tree AT[1000000];
AT[1] = T;
int n, l, r, height;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &l, &r);
if (l != 0)
{
temp = (Tree)malloc(sizeof(struct BTNode));
temp->left = NULL;
temp->right = NULL;
temp->x = AT[i]->x + 1;
AT[i]->left = temp;
AT[l] = temp;
if (temp->x > height)
height = temp->x;
}
if (r != 0)
{
temp = (Tree)malloc(sizeof(struct BTNode));
temp->left = NULL;
temp->right = NULL;
temp->x = AT[i]->x + 1;
AT[i]->right = temp;
AT[r] = temp;
if (temp->x > height)
height = temp->x;
}
}
printf("%d", height);
return 0;
}
BTNode *CreatTree(BTDataType x)
{
BTNode *temp = (BTNode *)malloc(sizeof(BTNode));
if (temp == NULL)
{
perror("CreatTree::malloc");
exit(-1);
}
temp->x = x;
temp->left = temp->right = NULL;
return temp;
}