#include<bits/stdc++.h>
#define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i)
using namespace std;
const int N=2005;
int n,x,y,ans;
int f[N],g[N],mx[N];
vector<int>e[N];
char s[N];
void dfs(int x,int no){
	g[x]=s[x]=='1';
	f[x]=0;
	for(int i:e[x])if(i!=no){
		dfs(i,x);
		g[x]+=g[i];
		f[i]+=g[i];
		f[x]+=f[i];
	}
	int ma=0,sum=0;
	for(int i:e[x])if(i!=no)
		ma=max(ma,f[i]),sum+=f[i];
	if(ma*2>sum){
		int d=0;
		for(int i:e[x])if(i!=no&&ma==f[i])
			d=mx[i];
		if(ma-d<=sum-ma)mx[x]=sum-(sum&1);else{
			mx[x]=(sum-ma)*2+d;
		}
	}else mx[x]=sum-(sum&1);
}
int main(){
	scanf("%d\n%s",&n,s+1);
	fo(i,2,n)cin>>x>>y,e[x].push_back(y),e[y].push_back(x);
	ans=2e9;
	fo(i,1,n){
		dfs(i,0);
		if(f[i]%2==0&&mx[i]==f[i])ans=min(ans,f[i]/2);
	}
	printf("%d",ans==2e9?-1:ans);
}