#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#define MN 2010

int h[MN], nxt[2 * MN], to[2 * MN], K = 0;
int f[MN], g[MN], siz[MN];
char s[MN]; int n;

void ins(int u, int v) {nxt[++K] = h[u]; h[u] = K; to[K] = v;}

void dfs(int x, int fa)
{
	siz[x] = (s[x] == '1');
	int Max = 0, id = -1;
	for(int i = h[x]; i; i = nxt[i])
	{
		if(to[i] == fa) continue;
		dfs(to[i], x);
		siz[x] += siz[to[i]];
		g[x] += g[to[i]] + siz[to[i]];
		if(f[to[i]] + siz[to[i]] > Max) id = to[i], Max = f[to[i]] + siz[to[i]];
	}
	int sum = 0;
	for(int i = h[x]; i; i = nxt[i])
	{
		if(to[i] == fa) continue;
		if(to[i] != id) sum += g[to[i]] + siz[to[i]];
	}
	if(sum >= Max) f[x] = g[x] & 1;
	else f[x] = Max - sum;
}

int main()
{
	scanf("%d%s", &n, s + 1);
	for(int i = 1; i < n; i++)
	{
		int a, b; scanf("%d%d", &a, &b);
		ins(a, b); ins(b, a);
	}
	
	int ans = 2e9;
	for(int i = 1; i <= n; i++)
	{
		memset(f, 0, sizeof(f));
		memset(g, 0, sizeof(g));
		dfs(i, 0);
		if(f[i] == 0) ans = std::min(ans, g[i] / 2);
	}
	if(ans >= 1e9) puts("-1");
	else printf("%d\n", ans);
}