#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <limits.h>
using namespace std;
typedef long long LL;
const int N=2010;

int n,u,v,ans;
int dp1[N],dp2[N],sz[N];
char s[N];
vector<int> G[N];

void dfs(int u,int f) {
	sz[u]=(s[u]=='1');
	vector<int> tmp;
	int mx=-1,p=-1,o1,o2,son=0;
	for(int v: G[u]) {
		if(v==f) continue;
		dfs(v,u);
		sz[u]+=sz[v];
		o1=dp1[v]+sz[v];
		o2=dp2[v]+sz[v];
		dp1[u]+=o1;
		if(o2>mx) {
			mx=o2;p=v;
		}
		son++;
	}
	if(son==0) return;
	
	o1=dp1[u]-(dp1[p]+sz[p]);
	if(mx>o1) {
		dp2[u]=mx-o1;
	}
	else {
		dp2[u]=(dp1[u]%2);
	}
}

int main() {
	scanf("%d %s",&n,s+1);
	for(int i=1;i<n;i++) {
		scanf("%d %d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	ans=-1;
	for(int u=1;u<=n;u++) {
		memset(sz,0,sizeof(sz));
		memset(dp1,0,sizeof(dp1));
		memset(dp2,0,sizeof(dp2));
		dfs(u,0);
		if(dp2[u]==0) {
			if(ans==-1 || dp1[u]/2<ans) {
				ans=dp1[u]/2;
			}
		}
	}
	printf("%d\n",ans);
}