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