import sys L = sys.stdin.read().splitlines() seed = L[0] L = L[2:] L = [ l.split() for l in L ] D = { tuple(k):v for [k,_,v] in L } def reprpairs(P): return " ".join(f"{a}{b}:{v}" for ((a,b),v) in P.items()) E = { K:0 for (K,_) in D.items() } for (a,b) in zip(seed, seed[1:]): E[(a,b)] += 1 #print(reprpairs(E)) for step in range(40): F = { K:0 for (K,_) in D.items() } for ((a,b),v) in E.items(): j = D[(a,b)] F[(a,j)] += v F[(j,b)] += v E = F #print(step+1,reprpairs(E)) from collections import defaultdict as dfd S = dfd(int) for ((a,b),v) in E.items(): S[a]+=v S[b]+=v S ={ k:(a+b) for (k,v) in S.items() for (a,b) in [divmod(v,2)] } print(S, max(S.values())-min(S.values()))