#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

const int N = 5010;
const int MOD = 1e9 + 7;

int n;
string s;
vector<int> graph[N];
int ans = 1e18;

int si[N], cnt[N];

void dfs(int v, int p)
{
    si[v] = 0;
    cnt[v] = (s[v] == '1');
    for (auto u : graph[v]) if (u != p)
    {
        dfs(u, v);
        cnt[v] += cnt[u];
        si[v] += si[u];
    }
    si[v] += cnt[v];
}

void check(int v)
{
    dfs(v, -1);
    //for (int i = 0; i < n; i++) cout << si[i] << " "; cout << '\n';
    int S = 0;
    for (auto u : graph[v]) S += si[u];
    if (S % 2 != 0) return;
    int bad = -1;
    for (auto u : graph[v])
    {
        if (si[u] > S / 2)
        {
            bad = u;
        }
    }
    //cout << S << " " << bad << " " << si[bad] << "\n";
    if (bad == -1)
        ans = min(ans, S / 2);
    int d = (si[bad] - S / 2);
    int p = v;
    int it = 2100;
    //cout << bad << "\n";
    while (d > 0 && it--)
    {
        int sz = 0;
        for (auto u : graph[bad]) if (u != p)
        {
            sz += si[u];
        }
        int b2 = -1;
        for (auto u : graph[bad]) if (u != p)
        {
            if (b2 == -1 || si[u] > si[b2])
                b2 = u;
        }
        if (b2 == -1) break;
        if (si[b2] > sz / 2) d -= sz - si[b2];
        else d -= sz / 2;
        p = bad;
        bad = b2;
    }
    if (d <= 0) ans = min(ans, S / 2);
    return;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    for (int i = 0; i + 1 < n; i++)
    {
        int a, b;
        cin >> a >> b, a--, b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    for (int i = 0; i < n; i++) check(i);
    if (ans >= 1e9)
    {
        cout << -1;
        return 0;
    }
    cout << ans;
}


Measured memory_bytes: 640