// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool smin(T &a, const T &b)   { return a > b ? a = b : a;    }
template<typename T> inline bool smax(T &a, const T &b)   { return a < b ? a = b : a;    }

typedef long long LL;

const int N = (int) 2005, mod = (int) 0;
int s_up[N], sz[N], a[N], yl[N], yr[N], xl[N], xr[N], sum[N];
vector<int> adj[N];
pair<int, int> merge(int lu, int ru, int lv, int rv) {
	int pl = max(lu, lv);
	int pr = min(ru, rv);
	int lx = max(0, pl - pr);
	int rx = ru + rv;
	if ((rx & 1) && (lx % 2 == 0)) {
		++lx;
	}
	return mp(lx, rx);
}
pair<int, int> merge(pair<int, int> x, pair<int, int> y) {
	return merge(x.x, x.y, y.x, y.y);	
}
pair<int, int> update(int lu, int ru, int s, int t = 1) {
	lu += s * t;
	ru += s * t;
	return mp(lu, ru);
}
void dfs_down(int v, int p = -1) {
	sz[v] = a[v];
	int &lv = yl[v], &rv = yr[v];
	vector<int> ch;
	for (int u : adj[v]) if (u != p) {
		ch.pb(u);
		dfs_down(u, v);
		sz[v] += sz[u];
		int lu = yl[u], ru = yr[u];
		auto x = update(lu, ru, sz[u], 1);
		lu = x.first, ru = x.second;
		auto e = merge(lv, rv, lu, ru);
		lv = e.first, rv = e.second;
		sum[v] += sum[u] + sz[u];
	}
//	adj[v] = ch;
}
void dfs_up(int v, int p = -1, int sum_up = 0, pair<int, int> pv = mp(0, 0)) {
	s_up[v] = sum_up + sum[v];
	int sum_all = 0;
	for (int u : adj[v]) {
		sum_all += sum[u] + sz[u];
	}
	sum_up += sz[0] - sz[v];
	pv = update(pv.first, pv.second, sz[0] - sz[v], 1);
	int m = (int) adj[v].size();
	vector<pair<int, int>> ls(m + 1), rs(m + 1);
	for (int j = 0; j < m; ++j) {
		int u = adj[v][j];
		int lu = yl[u], ru = yr[u];
		auto x = update(lu, ru, sz[u], 1);
		lu = x.x, ru = x.y;
		ls[j + 1] = merge(lu, ru, ls[j].x, ls[j].y);
	}
	for (int j = m - 1; j >= 0; --j) {	
		int u = adj[v][j];
		int lu = yl[u], ru = yr[u];
		auto x = update(lu, ru, sz[u], 1);
		lu = x.x, ru = x.y;
		rs[j] = merge(lu, ru, rs[j + 1].x, rs[j + 1].y);
	}
	for (int j = 0; j < m; ++j) {
		int u = adj[v][j];
		dfs_up(u, v, sum_all + sum_up - sum[u] - sz[u], merge(merge(pv, ls[j]), rs[j + 1]));	
	}
	auto x = merge(pv, rs[0]);
	xl[v] = x.first;
	xr[v] = x.second;
}
int main() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	for (int j = 0; j < n; ++j) a[j] = s[j] - '0';
	for (int j = 0; j < n - 1; ++j) {
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
//	dfs_down(0);
//	dfs_up(0);
	int res = 1e9;
	for (int j = 0; j < n; ++j) {
		memset(yl, 0, sizeof yl);
		memset(yr, 0, sizeof yr);
		memset(sum, 0, sizeof sum);
		memset(sz, 0, sizeof sz);
		dfs_down(j);
//		cout << " hi " << j << ' ' << yl[j] << ' ' << yr[j] << ' ' << sum[j] << endl;
		if (yl[j] == 0) {
			res = min(res, sum[j] / 2);//s_up[j] / 2);
		}
	}
	cout << (res > N * N + 100 ? -1 : res) << endl;
}



















Measured memory_bytes: 640