x, y, z = map(int, input().split()) s0, t0 = input().split() t0 = int(t0) s1, t1 = input().split() t1 = int(t1) if s0 == 'A': if s1 == 'A': ans = min(abs(t1-t0), t0+y+x-t1, t0+z+x-t1) elif s1 == 'B': ans = min(t0+t1-1, x-t0+y-t1+1, t0+z+y-t1) else: ans = min(t0+t1-1, x-t0+z-t1+1, t0+y+z-t1) elif s0 == 'B': if s1 == 'A': ans = min(t0+t1-1, y-t0+x-t1+1, t0+z+x-t1) elif s1 == 'B': ans = min(abs(t1-t0), t0+x+y-t1, t0+z+y-t1) else: ans = min(t0+t1-1, y-t0+z-t1+1, t0+x+z-t1) else: if s1 == 'A': ans = min(t0+t1-1, z-t0+x-t1+1, t0+y+x-t1) elif s1 == 'B': ans = min(t0+t1-1, z-t0+y-t1+1, t0+x+y-t1) else: ans = min(abs(t1-t0), t0+x+z-t1, t0+y+z-t1) print(ans)