// vvvvvvvvvvvv TEMPLATE vvvvvvvvvvvv
#include <bits/stdc++.h>
using namespace std; using ll = long long; using P = pair<ll, ll>;
const ll linf = 1e18; const double eps = 1e-12, pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define each(i,a) for (auto&& i : a)
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
template<typename T> ll min(ll a, const T&& b) { return a < b ? a : b; }
template<typename T> ll min(const T&& a, ll b) { return a < b ? a : b; }
template<typename T> ll max(ll a, const T&& b) { return a > b ? a : b; }
template<typename T> ll max(const T&& a, ll b) { return a > b ? a : b; }
template<typename Head> void out(Head h) { cout << h << endl; } template<typename Head, typename... Tail>void out(Head h, Tail... t) { cout << h << " "; out(t...); }
template<typename T> istream& operator>>(istream& is, vector<T>& v) { each(x,v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
ostream& operator<<(ostream& os, const vector<string>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; }
struct yes_no : std::numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } };
void solve(); int main() {
  ios::sync_with_stdio(false); cin.tie(0); locale loc(locale(), new yes_no); cout.imbue(loc); cout << fixed << setprecision(10) << boolalpha;
  solve();
}
// ^^^^^^^^^^^^ TEMPLATE ^^^^^^^^^^^^

class UnionFind {
  vector<ll> par, h;
public:
  UnionFind(ll size) : par(size, 0), h(size, 0) {
    rep(i, size) par[i] = i;
  }
  void unite(ll u, ll v) {
    u = root(u), v = root(v);
    if (u == v) return;
    if (h[u] < h[v]) {
      par[u] = v;
    }
    else {
      par[v] = u;
    }
    if (h[u] == h[v]) ++h[u];
  }
  bool isUnited(ll u, ll v) {
    return root(u) == root(v);
  }
  ll root(ll v) {
    if (par[v] == v) return v;
    return par[v] = root(par[v]);
  }
};

template<class Calc>
class EdgeDpOnForest {
public:
  using T = typename Calc::type;
  struct Edge {
    ll to, rev;
    T value;
  };
private:
  ll n;
  void dfs1(ll v, ll pv) {
    ll idx = -1;
    vector<T> values;
    rep(i, G[v].size()) {
      const Edge& e = G[v][i];
      if (e.to == pv) {
        idx = i;
        continue;
      }
      dfs1(e.to, v);
      values.pb(e.value);
    }
    // 根以外では親から入る辺の値を更新
    if (idx >= 0) {
      const Edge& e = G[v][idx];
      G[e.to][e.rev].value = Calc::merge(values, v);
    }
  }
  void dfs2(ll v, ll pv) {
    vector<T> values;
    each(e, G[v]) values.pb(e.value);
    values = Calc::evaluate(values, v);
    rep(i, G[v].size()) {
      const Edge& e = G[v][i];
      if (e.to == pv) continue;
      G[e.to][e.rev].value = values[i];
      dfs2(e.to, v);
    }
  }
public:
  UnionFind uf;
  vector<vector<Edge>> G;
  EdgeDpOnForest(ll n) : n(n), uf(n), G(n) {}
  void add(ll u, ll v) {
    assert(!uf.isUnited(u, v));
    G[u].pb({v, ll(G[v].size())});
    G[v].pb({u, ll(G[u].size())-1});
    uf.unite(u, v);
  }
  // 各有向辺の値 G[v][i].value を計算する
  void dp() {
    vector<bool> used(n, false);
    rep(i, n) {
      if (used[uf.root(i)]) continue;
      dfs1(i, -1);
      used[uf.root(i)] = true;
    }
    used.assign(n, false);
    rep(i, n) {
      if (used[uf.root(i)]) continue;
      dfs2(i, -1);
      used[uf.root(i)] = true;
    }
  }
  size_t size() const {
    return G.size();
  }
};

string s;

struct Data {
  ll sum, units, rest, step;
};

struct X {
  using type = Data;
  // その頂点から出ていく値から入ってくる値を計算する
  static vector<type> evaluate(const vector<type>& value, const ll id) {
    vector<type> res;
    rep(i, value.size()) {
      vector<type> nvalue;
      rep(j, value.size()) {
        if (i != j) nvalue.pb(value[j]);
      }
      res.pb(X::merge(nvalue, id));
    }
    return res;
  }
  // (辺数-1)個の出ていく値から1個の入ってくる値を計算する
  static type merge(const vector<type>& value, const ll id) {
    const ll n = value.size();
    ll sum = 0, units = 0, rest = 0, step = 0;
    vector<ll> rests;
    ll rest_sum = 0;
    each(p, value) {
      sum += p.sum;
      units += p.units;
      rests.pb(p.rest);
      rest_sum += p.rest;
      step += p.step;
    }
    if (rests.size() > 0) {
      ll mx = *max_element(all(rests));
      if (mx > rest_sum - mx) {
        rest = mx - (rest_sum - mx);
        step += rest_sum - mx;
      }
      else {
        rest = rest_sum % 2;
        step += rest_sum / 2;
      }
    }
    if (s[id] == '1') {
      ++units;
    }
    sum += units;
    rest += units;
    return {sum, units, rest, step};
  }
};

void solve() {
  ll n; cin >> n;
  cin >> s;
  EdgeDpOnForest<X> dp(n);
  rep(i, n-1) {
    ll u, v; cin >> u >> v; --u, --v;
    dp.add(u, v);
  }
  dp.dp();
  ll ans = linf;
  rep(i, n) {
    vector<Data> v;
    // cout << i+1 << " : ";
    each(p, dp.G[i]) {
      // cout << "(" << p.value.sum << ", " << p.value.units << ", " << p.value.rest << ", " << p.value.step << ") ";
      v.pb(p.value);
    }
    // cout << endl;
    ll sum = 0;
    rep(j, v.size()) sum += v[j].sum;
    if (sum % 2 != 0) continue;
    Data mx = *max_element(all(v), [](const Data& d1, const Data& d2) { return d1.sum < d2.sum; });
    if (mx.rest > sum - mx.sum) continue;
    chmin(ans, sum / 2);
  }
  if (ans == linf) {
    cout << -1 << endl;
  }
  else {
    cout << ans << endl;
  }
}


Measured memory_bytes: 1152