a, b, c = map(int,input().split()) S = input().split() s = int(S[1]) G = input().split() g = int(G[1]) if S[0] == G[0]: if s > g: s, g = g, s if S[0] == 'B': a, b, c = b, a, c elif S[0] == 'C': a, b, c = c, b, a x = min(g-s, s+c+a-g, s+b+a-g) else: if S[0] == 'A' and G[0] == 'B': a, b, c = a, c, b elif S[0] == 'B' and G[0] == 'A': a, b, c = b, c, a elif S[0] == 'B' and G[0] == 'C': a, b, c = b, a, c elif S[0] == 'C' and G[0] == 'A': a, b, c = c, b, a elif S[0] == 'C' and G[0] == 'B': a, b, c = c, a, b x = min(s+g-1, a-s+c-g+1, a-s+b+g, s+b+c-g) print(x)