from sys import stdin input = stdin.readline from random import randrange from heapq import heappush, heappop base1, base2 = 10**5*12, 12 def encode(d, n, c): return d*base1+n*base2+c base3 = 10**5 def decode(n): return n//base1, n//base2%base3, n%base2 N, M = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(M): u, v, c = map(int, input().split()) u, v = u-1, v-1 G[u].append((v, c)) S = input().rstrip("\n") INF = 1<<60 dist = [[INF]*12 for _ in range(N)] visited = [[False]*12 for _ in range(N)] def func(color): c = 0 if S[0] != "K" else 4 for i in range(N): for j in range(12): dist[i][j] = INF visited[i][j] = False dist[0][c] = 0 que = [encode(0, 0, c)] while que: d, n, c = decode(heappop(que)) if visited[n][c]: continue visited[n][c] = True for next, weight in G[n]: nc = c if nc//4 == 0 and S[next] == "K": nc += 4 elif nc//4 == 1 and S[next] == "C" and nc%4 == 0: nc |= 1<>color[next] & 1 == 0 and S[next] == "C": nc |= 1<