#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cassert>
using namespace std;

#define FILL(a, n, x) memset((a), (x), sizeof ((a)[0]) * (n))
#define COPY(a, n, b) memcpy((b), (a), sizeof ((a)[0]) * (n))

const int mod = 998244353;
typedef unsigned long long lu;
typedef long long li;

inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
inline int Mul(int x, int y) { return (lu)x * y % mod; }
inline int Mul(int x, int y, int z) { return (lu)x * y % mod * z % mod; }

inline int Pow(int x, int y) {
  int z = 1;
  for (; y; y >>= 1) {
    if (y & 1) z = Mul(z, x);
    x = Mul(x, x);
  }
  return z;
}

namespace mcmf {
  typedef int FlowType;
  typedef li CostType;
  const int maxn = 1e5, maxm = 2e5;
  const FlowType inff = ~0U >> 2;
  const CostType infc = ~0LLU >> 2;

  int n, m, s, t;
  vector<int> g[maxn];

  struct Edge {
    int v;
    FlowType cap, flow;
    CostType cost;
  } edge[maxm << 1];

  void Init(int n0, int s0, int t0) {
    n = n0; m = 0; s = s0; t = t0;
    for (int i = 0; i < n; ++i) {
      g[i].clear();
    }
  }

  void AddEdge(int u, int v, FlowType cap, CostType cost) {
    g[u].push_back(m);
    edge[m++] = (Edge){v, cap, 0, cost};
    g[v].push_back(m);
    edge[m++] = (Edge){u, 0, 0, -cost};
  }

  CostType dist[maxn], h[maxn];

  struct Node {
    int u;
    CostType d;
    bool operator < (const Node &rhs) const {
      return d > rhs.d;
    }
  };

  bool Dijkstra(void) {
    priority_queue<Node> q;
    q.push((Node){s, 0});
    fill(dist, dist + n, infc);
    dist[s] = 0;
    while (!q.empty()) {
      int u = q.top().u;
      CostType d = q.top().d;
      q.pop();
      if (d != dist[u]) continue;
      for (int i = 0; i < g[u].size(); ++i) {
        Edge &e = edge[g[u][i]];
        if (e.flow < e.cap) {
          CostType t = dist[u] + h[u] - h[e.v] + e.cost;
          if (dist[e.v] > t) {
            dist[e.v] = t;
            q.push((Node){e.v, t});
          }
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      h[i] = min(h[i] + dist[i], infc);
    }
    return h[t] != infc;
  }

  bool ins[maxn];
  int cur[maxn];

  FlowType Dfs(int u, FlowType f) {
    if (u == t) return f;
    ins[u] = true;
    FlowType sum = 0;
    for (int &i = cur[u]; i < g[u].size(); ++i) {
      Edge &e = edge[g[u][i]], &r = edge[g[u][i] ^ 1];
      if (e.flow < e.cap && e.cost + h[u] - h[e.v] == 0 && !ins[e.v]) {
        FlowType t = Dfs(e.v, min(f - sum, e.cap - e.flow));
        e.flow += t;
        r.flow -= t;
        sum += t;
        if (sum == f) break;
      }
    }
    ins[u] = false;
    return sum;
  }

  pair<FlowType, CostType> Solve(void) {
    pair<FlowType, CostType> ans(0, 0);
    fill(h, h + n, 0);
    fill(ins, ins + n, false);
    while (Dijkstra()) {
      fill(cur, cur + n, 0);
      FlowType f = Dfs(s, inff);
      ans.first += f;
      ans.second += f * h[t];
    }
    return ans;
  }
}

// void Init(int n0, int s0, int t0)
// void AddEdge(int u, int v, FlowType cap, CostType cost)
// pair<FlowType, CostType> Solve(void)

// ----------------------------------------

const int maxn = 2000;

int n;
vector<int> g[maxn];
char s[maxn + 1];
int L[maxn], R[maxn], S[maxn];
int dep[maxn];

void Dfs(int u, int p) {
  S[u] = s[u] - '0', R[u] = 0;
  int sum = 0, mx = 0, id = -1;
  for (int v : g[u]) {
    if (v == p) continue;
    dep[v] = dep[u] + 1;
    Dfs(v, u);
    S[u] += S[v];
    R[u] += R[v] + S[v];
    sum += L[v] + S[v];
    // mx = max(mx, L[v] + S[v]);
    if (L[v] + S[v] > mx) {
      mx = L[v] + S[v];
      id = v;
    }
  }

  if (mx * 2 <= sum) {
    L[u] = 0;
    if (R[u] & 1) L[u] = 1;
  } else {

    int sumr = mx;
    for (int v : g[u]) {
      if (v == p || v == id) continue;
      sumr += R[v] + S[v];
    }
    if (mx * 2 <= sumr) {
      L[u] = 0;
      if (R[u] & 1) L[u] = 1;
    } else {
      L[u] = mx - (sumr - mx);
    }
  }
}

int Solve(int rt) {
  dep[rt] = 0;
  Dfs(rt, -1);
  if (L[rt] == 0) {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] == '1') sum += dep[i];
    }
    assert(sum % 2 == 0);
    return sum >> 1;
  } else return 1e9;
}

int main(void) {
  scanf("%d%s", &n, s);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u; --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 1e9;
  for (int u = 0; u < n; ++u) {
    ans = min(ans, Solve(u));
  }
  if (ans > 1e8) ans = -1;
  printf("%d\n", ans);
}


Measured memory_bytes: 4224