#define P(A) A=-~A
#define fione(i,a,b) for(register int i=a;i<=b;P(i))
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
typedef long long LL;
typedef unsigned long long ULL;
namespace FastIo {
typedef __uint128_t ULLL;
static char buf[100000], *p1 = buf, *p2 = buf, fw[100000], *pw = fw;
#define gc p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++
inline void pc(const char &ch) {
if (pw - fw == 100000)
fwrite(fw, 1, 100000, stdout), pw = fw;
*pw++ = ch;
}
#define fsh fwrite(fw,1,pw-fw,stdout),pw=fw
struct FastMod {
FastMod(ULL b): b(b), m(ULL((ULLL(1) << 64) / b)) {}
ULL reduce(ULL a) {
ULL q = (ULL)((ULLL(m) * a) >> 64);
ULL r = a - q * b;
return r >= b ? r - b : r;
}
ULL b, m;
} HPOP(10);
struct QIO {
char ch;
int st[40];
bool pd;
template<class T>inline void read(T &x) {
x = 0, ch = gc, pd = false;
while (!isdigit(ch)) {
pd = ch == '-' ? true : false;
ch = gc;
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = gc;
}
if (pd)
x = -x;
}
template<class T>inline void write(T a) {
if (a < 0)
a = -a, pc('-');
do {
st[++st[0]] = HPOP.reduce(a);
a /= 10;
} while (a);
while (st[0])
pc(st[st[0]--] ^ 48);
}
} qrw;
}
using namespace FastIo;
const int NUMBER1 = 1e6;
struct NODE {
int l, r;
LL v;
} tr[NUMBER1 + 5];
bool check(int l, int r) {
if (!(~l) && !(~r))
return true;
if (!(~l) || !(~r) || tr[l].v != tr[r].v)
return false;
return check(tr[l].l, tr[r].r) && check(tr[l].r, tr[r].l);
}
LL sum(int u) {
return ~u ? sum(tr[u].l) + sum(tr[u].r) + 1 : 0;
}
signed main() {
int n;
LL ans(0);
qrw.read(n);
fione(i, 1, n)qrw.read(tr[i].v);
fione(i, 1, n) {
qrw.read(tr[i].l);
qrw.read(tr[i].r);
}
fione(i, 1, n)if (check(i, i))
ans = std::max(ans, sum(i));
qrw.write(ans);
fsh;
exit(0);
return 0;
}