#include<bits/stdc++.h>
using namespace std;
typedef struct {
int l, r;
}TR;
typedef TR* qtr;
TR tree[1000050];
int dfs(TR q[100005],int t) {
int trl; int trr;
if (q[t].l == 0 && q[t].r == 0) {
return 1;
}if (q[t].l != 0) {
trl=dfs(q, q[t].l);
}
else if (q[t].r != 0) {
trr=dfs(q, q[t].r);
}
return max(trl, trr)+1;
}
int main() {
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> tree[i].l >> tree[i].r;
}
int ans = dfs(tree, 1);
cout << ans << endl;
return 0;
}