#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e6+1;
#define int unsigned long long
int n,m;
int a[maxn],son[maxn][2];
int val[maxn],sz[maxn];
int base=913;
int qpow(int x,int b) {
int ans=1,k=x;
while(b) {
if(b%=2) {
ans*=k;
}
k*=k;
b/=2;
}
return ans;
}
int ans=1;
void dfs(int now,int dep) {
val[now]=a[now]*qpow(base,dep);
sz[now]=1;
int cnt=7;
for(int i=0; i<=1; i++) {
int v= son[now][i];
if(v==-1||v==0)continue;
dfs(v,dep+1);
val[now]+=val[v]*cnt;
sz[now]+=sz[v];
cnt+=10;
}
if(val[son[now][0]]==val[son[now][1]]) {
ans=max(ans,sz[now]);
}
return ;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1; i<=n; i++) {
cin>>a[i];
}
for(int i=1; i<=n; i++) {
cin>>son[i][0]>>son[i][1];
}
dfs(1,1);
cout<<ans;
return 0;
}