#include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i ++)
#define re0(i, n) for (rint i = 0; i < (int) n; i ++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint int
using namespace std;
     
typedef long long lo;
     
template<typename tp> inline void read(tp &x) {
	x = 0; char c = getchar(); int f = 0;
	for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
	for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
	if (f) x = -x;
}
#define int long long
const int N = 2333;
vector <int> G[N];
char str[N];
int dep[N], n;
lo sum, _total;

pair <int, int> dfs(int u, int fat) {
	int sum = 0, size = str[u], total = 0;
	dep[u] = dep[fat] + 1;
	vector <int> cur;
	for (int v : G[u]) {
		if (v != fat) {
			pair <int, int> t = dfs(v, u);
			cur.push_back(t.first + t.second);
			size += t.second;
			total += t.first + t.second;
		}
	}
	if (!cur.empty()) {
		sort(cur.begin(), cur.end());
		sum = max(total & 1, cur.back() * 2 - total);
	}
	_total += (total - sum) / 2;
	return make_pair(sum, size);
}

inline lo work(int u) {
	_total = 0;
	dep[0] = -1;
	pair <int, int> t = dfs(u, 0);
	if (t.first) return 1e18;
	else return _total;
}

signed main(void) {
	read(n);
	scanf("%s", str + 1);
	rep (i, n) str[i] -= '0';
	rep (i, n - 1) {
		int x, y; read(x); read(y);
		G[x].push_back(y); G[y].push_back(x);
	}
	lo ans = 1e18;
	rep (i, n) ans = min(ans, work(i));
	cout << (ans > 1e17 ? -1 : ans) << "\n";
}
