#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL N = 3005;

LL ec = 0,hed[N] = {0},nxt[N << 1],to[N << 1];
void add_edge(LL f,LL t){
	++ ec; to[ec] = t; nxt[ec] = hed[f]; hed[f] = ec;
}

LL dep[N],siz[N],l[N],r[N];
LL s[N],n; string t;
void dfs(LL u,LL f){
	LL sr = 0,id = 0;
	dep[u] = dep[f] + 1;
	if(s[u]) siz[u] = 1;
	for(LL i = hed[u];i;i = nxt[i]){
		LL v = to[i];
		if(v == f) continue;
		dfs(v,u);
		siz[u] += siz[v];
	}
	for(LL i = hed[u];i;i = nxt[i]){
		LL v = to[i];
		if(v == f) continue;
		l[v] += siz[v]; r[v] += siz[v];
		sr += r[v];
		if(l[v] > l[id]) id = v;
	}
	l[u] = max(0LL,l[id] + r[id] - sr); r[u] = sr;
}

LL solve(LL x){
	LL sum = 0; dep[0] = -1;
	for(LL i = 1;i <= n;i ++) l[i] = r[i] = siz[i] = 0;
	dfs(x,0);
	for(LL i = 1;i <= n;i ++) if(s[i]) sum += dep[i];
	if(l[x] == 0 && sum % 2 == 0) return sum / 2;
	return INF;
}

int main(){
	ios::sync_with_stdio(false);
	LL u,v,ans = INF;
	cin >> n;
	cin >> t; for(LL i = 0;i < n;i ++) s[i + 1] = t[i] - '0';
	for(LL i = 1;i < n;i ++){
		cin >> u >> v;
		add_edge(u,v);
		add_edge(v,u);
	}
	for(LL i = 1;i <= n;i ++) ans = min(ans,solve(i));
	if(ans == INF) ans = -1;
	cout << ans << '\n';
	return 0;
}
//

