import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 n = II() s = [ord(c)-97 for c in SI()] m = len(s) to = [[] for _ in range(n)] for _ in range(n-1): u, v, c = SI().split() u = int1(u) v = int1(v) c = ord(c)-97 to[u].append((v, c)) to[v].append((u, c)) parent, depth, vis = [-1]*n, [0]*n, [0]*n cc = [""]*n def dfs(root): uu = [] vis[root] = 1 stack = [root] while stack: iu = stack.pop() i, u = divmod(iu, n) if i == 0: uu.append(u) # care to's format while i < len(to[u]) and vis[to[u][i][0]]: i += 1 if i < len(to[u]): stack.append((i+1)*n+u) # care to's format v, c = to[u][i] vis[v] = 1 stack.append(v) parent[v] = u depth[v] = depth[u]+1 cc[v] = c return uu uu = dfs(0) def dodp(s): def chmax(i, j, val): if val > dp[i][j]: dp[i][j] = val dp = [[0]*(m+1) for _ in range(n)] for u in uu[::-1]: if u == 0: break if len(to[u]) == 1: val = 0 for i, c in enumerate(s): if c == cc[u]: val = 1 dp[u][i+1] = val p = parent[u] for i, c in enumerate(s): pre = dp[u][i] if c == cc[p]: chmax(p, i+1, pre+1) elif pre: chmax(p, i, pre) if i+1 <= m: chmax(u, i+1, pre) return dp dp = dodp(s) rdp = dodp(s[::-1]) # pDB(rdp) ans = 0 for u in range(n): for i in range(m+1): m1 = m2 = 0 for v, _ in to[u]: if v == parent[u]: continue v1, v2 = dp[v][i], rdp[v][m-i] if v1+m2 > ans: ans = v1+m2 if v2+m1 > ans: ans = v2+m1 if v1 > m1: m1 = v1 if v2 > m2: m2 = m2 print(ans)