#include "bits/stdc++.h"
#define YES "YES"
#define Yes "Yes"
#define NO "NO"
#define No "No"
#pragma GCC optimize ("O3")
#define YESNO OUT(three(solve(),YES,NO))
#define YesNo OUT(three(solve(),Yes,No))
#define ECHO OUT(solve())
#define three(A,B,C) ((A)?(B):(C))
#define FOR(i,a,b)  for(LL i=(a);i< (LL)(b);i++)
#define EFOR(i,a,b) for(LL i=(a);i<=(LL)(b);i++)
#define RFOR(i,a,b) for(LL i=(a);i>=(LL)(b);i--)
#define REP(i,b) FOR(i,zero,b)
#define EREP(i,b) EFOR(i,zero,b)
#define RREP(i,b) RFOR(i,b-1,zero)
#define ALL(c) c.begin(),c.end()
#define UNIQUE(c) sort(ALL(c));c.erase(unique(ALL(c)),c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB emplace_back
#define DEBUG(a) OUT(a)
#define DEBUG2(a,b) OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a,b) cout << (a) <<" "<<(b) << endl
#define int long long
#define zero 0LL
#define pb PB
using namespace std;
template<typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; }
template<class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; }
template<class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; }
typedef long long LL;
typedef long double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr> > PQ;
inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }OUT(""); }
int dx[] = { 0,1,0,-1,1,1,-1,-1,0 }, dy[] = { 1,0,-1,0,1,-1,1,-1,0 };
const int SIZE1 = 5e5 + 1000;
const int SIZE2 = 2010;
const int SIZE3 = 188;
const int SIZE = SIZE2;
const LL p = 7 + 1e9;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
//ut N, M, K, L=0,R=1, H, W, Q, D, T;
ut N,A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U;
VI edges[SIZE];
//LL A[SIZE][SIZE];
string s;
VI vals[SIZE];
LL sum[SIZE];
bool ables[SIZE];
typedef tuple<int,int,int> tup3;
tup3 dfs(int now, int from, int dist, int root) {
	vals[root].pb(dist);

	//cout << now << " " << from << endl;
	LL times = 0;
	if (s[now] == '1') {
		times++;
		sum[root] += dist;
	}
	if (now == root) {
		LL ans = 0;
		LL maxim = 0;
		LL penalty = 0;
		for (auto next : edges[now]) {
			if (next == from) continue;
			auto c = dfs(next, now, dist + 1, root);
			
			if (MX(maxim, get<0>(c))) {
				penalty = get<1>(c);
			}
			ans += get<1>(c);
			
		}
		ans -= penalty;
		if (maxim > ans) ables[now] = false;
		else if (sum[now] % 2 == 1) ables[now] = false;
		else ables[now] = true;
		return tup3(0,0,0);
	}
	else {
		LL maxim = 0,minium = 0;
		LL maxer = 0;
		LL ans = 0;
		LL penalty = 0;
		for (auto next : edges[now]) {
			if (next == from) continue;
			auto c= dfs(next, now, dist + 1, root);
			times += get<2>(c);
			maxim += get<1>(c);
			if (MX(maxer, get<0>(c))) {
				penalty = get<1>(c);
			}
			ans += get<1>(c);

		}
		minium = max(maxim % 2, maxer-(ans-penalty));
	//	cout <<root <<" "<<now <<" "<< minium << " " << maxim << " " << times << endl;
		return tup3(minium+times, maxim+times,times);
	}
	
}
LL solve() {
	cin >> N;
	cin >> s;
	
	REP(i, N - 1) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		edges[a].pb(b);
		edges[b].pb(a);
	}
	REP(i, N) {
		LL c = 0;
		dfs(i,-1,0,i);
	}
	bool able = false;
	LL ans = INF;
	REP(i, N) {
		if (ables[i]) {
			MN(ans, sum[i] / 2);
			able = true;
		}
		
	}
	if (!able) cout << -1 << endl;
	else cout << ans << endl;
	return 0;
}
signed main() {

	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	solve();
	//	cin >> N;
	return 0;
}


Measured memory_bytes: 32640