#import sys #input = sys.stdin.readline def main(): x, y, z = map( int, input().split()) s0, t0 = input().split() s1, t1 = input().split() t0, t1 = int(t0), int(t1) if t0 > t1: s0, s1 = s1, s0 t0, t1 = t1, t0 if s0 != "A": if s0 == s1: if s0 == "B": x, y = y, x else: x, z = z, x s0 = s1 = "A" else: if s1 == "B": x, z = z, x else: x, y = y, x s0 = "A" if s1 == "C": y, z = z, y s1 = "B" # print(x,y,z,s0,t0,s1,t1) if s0 == s1: print( min(t1 - t0, t0 + x+1-t1 + min(y-1, z-1))) else: # print( min(t0, x-t0 + z) + t1-1, min(x+1-t0, t0+z) +y-t1) print( min( min(t0, x+1-t0 + z) + t1-1, min(x+1-t0, t0+z) +y-t1)) if __name__ == '__main__': main()