Balanced Tree

Given a tree whose vertices each hold A or B, swap characters along edges so no edge joins equal letters, using the fewest swaps, or report -1.

Medium6TreeDFSGreedyImplementationNo attempts yetTime limit1sMemory limit512 MB

Problem

A tree is a structure of nn vertices numbered 11 through nn and n1n - 1 edges placed so that exactly one path runs between any two vertices. Each vertex holds exactly one character, either the capital letter A or the capital letter B.

The tree is balanced if no edge joins two vertices that hold the same letter. You can try to balance the tree with a sequence of steps. In one step you choose one edge and swap the characters written in the two vertices that the edge joins.

Determine the minimum number of steps needed to balance the given tree.

Input

The first line contains the number of vertices nn (1n3000001 \le n \le 300\,000).

The second line contains a string of nn characters, each of them the capital letter A or the capital letter B. The jj-th character of the string is the character initially written in vertex jj.

Each of the next n1n - 1 lines contains two different positive integers xx and yy (1x,yn1 \le x, y \le n), the labels of two vertices joined directly by an edge. The vertices and edges form a tree as described above.

Output

Print the minimum number of steps. If the tree cannot be balanced, print -1.

The answer can exceed the range of a 32-bit integer.