#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n, C[2010], Res = 1e9;
struct AA {
	int b, e;
}D[2010];
char p[2010];
vector<int>E[2010];
void DFS(int a, int pp) {
	D[a].b = D[a].e = 0;
	C[a] = p[a] - '0';
	for (auto &x : E[a]) {
		if (x == pp)continue;
		DFS(x, a);
		C[a] += C[x];
		D[a].e += D[x].e + C[x];
	}
	vector<AA>TP;
	for (auto &x : E[a]) {
		if (x == pp)continue;
		TP.push_back({ D[x].b + C[x], D[x].e + C[x] });
	}
	if (TP.empty())return;
	int pv = -1, Mx = -1;
	for (int i = 0; i < TP.size(); i++) {
		if (Mx < TP[i].e) {
			Mx = TP[i].e;
			pv = i;
		}
	}
	int s = 0;
	for (int i = 0; i < TP.size(); i++) {
		if (i == pv)continue;
		s += TP[i].e;
	}
	D[a].b = max(TP[pv].b - s, D[a].e%2);
}
void Go(int a) {
	DFS(a, 0);
	if (D[a].b == 0) {
		Res = min(Res, D[a].e);
	}
}
int main() {
	int i, a, b;
	scanf("%d", &n);
	scanf("%s", p + 1);
	for (i = 1; i < n; i++) {
		scanf("%d%d", &a, &b);
		E[a].push_back(b);
		E[b].push_back(a);
	}
	for (i = 1; i <= n; i++) {
		Go(i);
	}
	if (Res > 8e8)puts("-1");
	else printf("%d\n", Res/2);
}