//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
//#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
//#pragma GCC target("avx2")  //Enable AVX
#include<bits/stdc++.h>
using namespace std;

#define all(a) begin(a),end(a)
#define F first
#define S second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int mod=998244353;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int mul(int a,int b){
  return ((a)*1ll*(b))%mod;
}

void add(int &a,int b){
  a+=b;
  if(a>=mod)a-=mod;
}

int sub(int a,int b){
  a-=b;
  if(a<0){
    a+=mod;
  }
  return a;
}

int powz(int a,int b){
  int res=1;
  while(b){
    if(b&1){
      res=mul(res,a);
    }
    b/=2;
    a=mul(a,a);
  }
  return res;
}

template <typename A, typename B>
istream& operator>>(istream& input,pair<A,B>& x) {
  input>>x.F>>x.S;
  return input;
}

template <typename A>
istream& operator>>(istream& input,vector<A>& x) {
  for(auto& i:x)
    input>>i;
  return input;
}

template<typename A>
ostream& operator<<(ostream& output,vector<A>& x) {
  for(auto& i:x)
    output<<i<<' ';
  return output;
}

template<typename T>
vector<pair<T,int>> getvec(int n){
  vector<pair<T,int>>a(n);
  for(int i=0;i<a.size();i++){
    cin>>a[i].F;
    a[i].S=i;
  }
  return a;
}

void clr(auto &a,int n){
  a.clear();
  a.resize(n);
}

void unq(auto &a){
  sort(all(a));
  a.resize(unique(all(a))-a.begin());
}

const int N=2005;

vector<int>adj[N];
int sz[N],low[N],high[N];

void dfs_sz(int u,int p,string &s){
  sz[u]=(s[u]=='1');
  for(auto i:adj[u]){
    if(i!=p){
      dfs_sz(i,u,s);
      sz[u]+=sz[i];
    }
  }
}

int gett(int u,int p,string &s,int dep){
  int val=0;
  if(s[u]=='1'){
    val+=dep;
  }
  for(auto i:adj[u]){
    if(i!=p){
      val+=gett(i,u,s,dep+1);
    }
  }
  return val;
}

void check(int u,int p,string &s,int dep){
  int sum=0,max_min=-1,max_max=-1;
  for(auto i:adj[u]){
    if(i!=p){
      check(i,u,s,dep+1);
      high[u]+=high[i]+sz[i];
      if(low[i]+sz[i]>max_min){
	max_min=low[i]+sz[i];
	max_max=high[i]+sz[i];
      }
      sum+=high[i]+sz[i];
    }
  }
  if(max_min>sum-max_max){
    low[u]=max_min-(sum-max_max);
  }
  else{
    low[u]=((sum)%2);
  }
}

void solve(){
  int n;
  cin>>n;
  string s;
  cin>>s;
  for(int i=0;i<n-1;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  int op=1e9;
  for(int i=0;i<n;i++){
    debug(i);
    memset(sz,0,sizeof(sz));
    memset(low,0,sizeof(low));
    memset(high,0,sizeof(high));
    dfs_sz(i,-1,s);
    vi zz;
    for(auto j:adj[i]){
      int gt=gett(j,i,s,1);
      if(sz[j]){
	zz.pb(gt);
      }
    }
    sort(all(zz));
    int sum=accumulate(all(zz),0);
    if(zz.size()==0){
      op=0;
      break;
    }
    if(zz.size()==1){
      continue;
    }
    if(sum%2){
      continue;
    }
    check(i,-1,s,0);
    if(low[i]==0){
      op=min(op,sum/2);
    }
  }
  if(op==1e9){
    op=-1;
  }
  cout<<op;
  
}

signed main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc=1;
  //cin>>tc;
  for(int _=0;_<tc;_++){
    // cout<<"Case #"<<_+1<<": ";
    solve();
    if(_!=tc-1){
      cout<<'\n';
    }
  }
}


Measured memory_bytes: 640