a, b, c = map(int,input().split()) S = input().split() s = int(S[1]) G = input().split() g = int(G[1]) l = [] 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 l.append(g-s) l.append(s+c+(a-g)) l.append(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 l.append((s+g-1)) l.append(((a-s)+(c-g)+1)) l.append(((a-s)+b+g)) l.append((s+b+(c-g))) print(min(l))