import sys
sys.setrecursionlimit(10 ** 5)
def solve():
  n = int(input())
  s = input()
  edges = [[] for _ in range(n)]
  def dfs(curr, p, l):
    ret = 0
    c = s[curr]
    if c == ')' and (not l or l[-1] != '('): return 0
    if c == ']' and (not l or l[-1] != '['): return 0
    if c == '}' and (not l or l[-1] != '{'): return 0
    if c == '}' or c == ']' or c == ')': l.pop()
    else: l.append(c)
    if not l: ret += 1
    for nxt in edges[curr]:
      if nxt != p: ret += dfs(nxt, curr, l)
    if c == ')': l.append('(')
    elif c == ']': l.append('[')
    elif c == '}': l.append('{')
    else: l.pop()
    return ret
  for _ in range(n-1):
    a, b = (int(x)-1 for x in input().split())
    edges[a].append(b)
    edges[b].append(a)
  ret = 0
  for _ in range(n):
    ret += dfs(_, -1, [])
  print(ret)

solve()