#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=2e3+5;
int n,dp[N],sum[N];
char num[N];
vector <int> son[N];

template <typename _Tp> inline void IN(_Tp&x) {
	char ch;bool flag=0;x=0;
	while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	if(flag) x=-x;
}
inline void addedge(int u=0,int v=0) {
	IN(u),IN(v),son[u].push_back(v),son[v].push_back(u);
}

void dfs(int u,int fa) {
	dp[u]=0,sum[u]=num[u]-'0';
	for(auto v:son[u]) if(v!=fa)
		dfs(v,u),sum[u]+=sum[v],dp[u]+=dp[v]+sum[v];
}
int solve(int u,int fa) {
    if(!u) return 0;
	int mx=0;
	for(auto v:son[u]) if(v!=fa&&dp[v]+sum[v]>dp[mx]+sum[mx]) mx=v;
	int tmp=solve(mx,u)+sum[mx];
	if(dp[u]-dp[mx]-sum[mx]>=tmp) return dp[u]&1;
	else return tmp-(dp[u]-dp[mx]-sum[mx]);
}

int main() {
	IN(n),scanf("%s\n",num+1);
	for(int i=2;i<=n;++i) addedge();
	int ans=1e9;
	for(int i=1;i<=n;++i) {
		dfs(i,0);
		if(!solve(i,0)) ans=min(ans,dp[i]>>1);
	}
	printf("%d\n",ans==1e9?-1:ans);
	return 0;
}