x, y, z = map(int,input().split()) S0, t0 = input().split() S1, t1 = input().split() t0, t1 = int(t0), int(t1) if S0 == S1 == "A": print(min(abs(t0-t1),x+y-abs(t0-t1))) elif S0 == S1 == "B": print(min(abs(t0-t1),x+y-abs(t0-t1),y+z-abs(t0-t1))) elif S0 == S1 == "C": print(min(abs(t0-t1),z+y-abs(t0-t1))) elif S0 == "A" and S1 == "B": print(min(t0+t1-1,x+y-(t0+t1-1),t0+z+y-t1,x-t0+z+t1)) elif S0 == "B" and S1 == "A": print(min(t0+t1-1,x+y-(t0+t1-1),t1+z+y-t0,x-t1+z+t0)) elif S0 == "C" and S1 == "B": print(min(t0+t1-1,z+y-(t0+t1-1),t1+x+z-t0,y-t1+x+t0)) elif S0 == "B" and S1 == "C": print(min(t0+t1-1,z+y-(t0+t1-1),t0+x+z-t1,y-t0+x+t1)) elif S0 == "A" and S1 == "C": print(min(t0+t1-1,z+x-(t0+t1-1),t0+z-t1+y,x-t0+t1+y)) else: print(min(t0+t1-1,z+x-(t0+t1-1),t1+z-t0+y,x-t1+t0+y))