Problem: You are given a tree with N vertices numbered 1, 2, ..., N. The i-th edge connects Vertex a_i and Vertex b_i. You are also given a string S of length N consisting of `0` and `1`. The i-th character of S represents the number of pieces placed on Vertex i.

Snuke will perform the following operation some number of times:

* Choose two pieces the distance between which is at least 2, and bring these pieces closer to each other by 1. More formally, choose two vertices u and v, each with one or more pieces, and consider the shortest path between them. Here the path must contain at least two edges. Then, move one piece from u to its adjacent vertex on the path, and move one piece from v to its adjacent vertex on the path.



By repeating this operation, Snuke wants to have all the pieces on the same vertex. Is this possible? If the answer is yes, also find the minimum number of operations required to achieve it.

Constraints

* 2 \leq N \leq 2000
* |S| = N
* S consists of `0` and `1`, and contains at least one `1`.
* 1 \leq a_i, b_i \leq N(a_i \neq b_i)
* The edges (a_1, b_1), (a_2, b_2), ..., (a_{N - 1}, b_{N - 1}) forms a tree.

Input

Input is given from Standard Input in the following format:


N
S
a_1 b_1
a_2 b_2
:
a_{N - 1} b_{N - 1}


Output

If it is impossible to have all the pieces on the same vertex, print `-1`. If it is possible, print the minimum number of operations required.

Examples

Input

7
0010101
1 2
2 3
1 4
4 5
1 6
6 7


Output

3


Input

7
0010110
1 2
2 3
1 4
4 5
1 6
6 7


Output

-1


Input

2
01
1 2


Output

0

Solution (in PYTHON3):

Solution:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    n = I()
    s = S()
    aa = [LI_() for _ in range(n-1)]
    e = collections.defaultdict(set)
    for a,b in aa:
        e[a].add(b)
        e[b].add(a)

    sa = []
    for i in range(n):
        if s[i] == '1':
            sa.append(i)

    if len(sa) <= 1:
        return 0

    m = {}
    def f(i,p):
        k = (i,p)
        if k in m:
            return m[k]

        t = 0
        if s[i] == '1':
            t = 1

        h = []
        tk = 0
        tc = 0
        for c in e[i]:
            if c == p:
                continue
            r = f(c,i)
            if r[0] < 1:
                continue
            tc += r[0]
            tk += r[0] + r[1]
            h.append([r[0]+r[1],r[2]])

        if p == -1:
            return h

        mk = 0
        mv = 0
        for u,v in h:
            if mk < u:
                mk = u
                mv = v
        if mk > tk - mk:
            kk = tk - mk
            mk -= kk
            kk += min(mv//2,mk)
            m[k] = (tc+t,tk,kk*2)
        else:
            m[k] = (tc+t,tk,tk//2*2)

        return m[k]

    r = inf

    for i in range(n):
        t = f(i,-1)
        tk = 0
        mk = 0
        for u,v in t:
            tk += u
        if tk % 2 == 1:
            continue

        ff = 1
        for u,v in t:
            kk = u-v
            nk = tk - u
            if kk > nk:
                ff = 0
        # print(i,ff,tk,mk,t)
        if ff == 0:
            continue
        if tk // 2 < mk:
            continue
        v = tk // 2
        if r > v:
            r = v

    if r == inf:
        return -1

    return r


print(main())