#include<bits/stdc++.h>
#define M 50005
using namespace std;
using ull = unsigned long long;
int n, ans[M];
ull hp[M][800];
void add(int x, int y)
{
for(int i = 0; i <= n / 63; i++)
{
hp[y][i] += (hp[x][i] >> 1);
if(hp[x][i + 1] & 1) hp[y][i] += (1ull << 62);
if(hp[y][i] >> 63) hp[y][i + 1]++, hp[y][i] ^= (1ull << 63);
}
}
int ctz(int i)
{
for(int j = 0; j <= n / 63; j++) if(hp[i][j]) return j * 63 + __builtin_ctzll(hp[i][j]);
}
int main()
{
scanf("%d", &n);
hp[1][n / 63] = 1ull << (n % 63);
int minn = n;
for(int i = 1, x; i <= n; i++)
{
scanf("%d", &x), add(i, x);
scanf("%d", &x), add(i, x);
minn = min(minn, ctz(i));
}
int pw = n - minn + 1;
ans[0] = 1;
int tot = 1;
while(pw)
{
int nt = min(pw, 25);
pw -= nt;
for(int i = 0; i < tot; i++) ans[i] <<= nt;
for(int i = 0; i < tot; i++) ans[i + 1] += ans[i] / 10, ans[i] %= 10;
while(ans[tot]) ans[tot + 1] += ans[tot] / 10, ans[tot] %= 10, tot++;
}
for(int i = tot - 1; i >= 0; i--) printf("%d", ans[i]);
return 0;
}