#include <bits/stdc++.h>

using namespace std;

const int MX = 2000, INF = 1e9;

char s[MX + 1];
vector<int> G[MX];

tuple<int, int, int> dfs(int v, int p = -1) {
	int mn = 0, mx = 0, cnt = 0;
	if (s[v] == '1') cnt++;
	
	for (int u : G[v]) {
		if (u == p) continue;
		int mnU, mxU, cntU;
		tie(mnU, mxU, cntU) = dfs(u, v);
		
		mnU += cntU;
		mxU += cntU;
		
		mn = max(mnU + mxU, mn);
		mx += mxU;
		cnt += cntU;
	}
	
	mn = max(mn - mx, 0);
	
	return make_tuple(mn, mx, cnt);
}

int main() {
	int n;
	ignore = scanf("%d %s", &n, s);
	for (int i = 1, u, v; i < n; i++) {
		ignore = scanf("%d %d", &u, &v);
		u--; v--;
		G[u].emplace_back(v);
		G[v].emplace_back(u);
	}
	
	int ans = INF;
	for (int v = 0; v < n; v++) {
		int mn, mx;
		tie(mn, mx, ignore) = dfs(v);
		if (mn == 0 && mx % 2 == 0) ans = min(ans, mx / 2);
	}
	
	if (ans == INF) ans = -1;
	printf("%d\n", ans);
	
	return 0;
}
