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