import unittest def check_constants(x, y, z, s0, t0, s1, t1): assert 1 <= x <= 10**9 assert 1 <= y <= 10**9 assert 1 <= z <= 10 ** 9 if s0 == 'A': assert 1 <= t0 <= x elif s0 == 'B': assert 1 <= t0 <= y elif s0 == 'C': assert 1 <= t0 <= z else: assert False, 'unreachable' if s1 == 'A': assert 1 <= t1 <= x elif s1 == 'B': assert 1 <= t1 <= y elif s1 == 'C': assert 1 <= t1 <= z else: assert False, 'unreachable' def get_route_length(route, x, y, z): if route == 'A': return x elif route == 'B': return y else: return z def solve(x, y, z, s0, t0, s1, t1): min_cost = float('inf') if s0 == s1: min_cost = min( abs(t1 - t0), t0 + min(x, y, z) + get_route_length(s1, x, y, z) - t1 ) else: min_cost = min( t0 + t1 - 1, get_route_length(s0, x, y, z) - t0 + get_route_length(s1, x, y, z) - t1 + 1, t0 + min(x, y, z) + get_route_length(s1, x, y, z) - t1, t1 + min(x, y, z) + get_route_length(s0, x, y, z) - t0, ) return min_cost def main(): x, y, z = map(int, input().split()) s0, t0 = input().split() s1, t1 = input().split() t0 = int(t0) t1 = int(t1) check_constants(x, y, z, s0, t0, s1, t1) result = solve(x, y, z, s0, t0, s1, t1) print(result) class Test3084(unittest.TestCase): def test3084(self): self.assertEqual(solve(3, 4, 5, 'A', 1, 'B', 1), 1) self.assertEqual(solve(3, 4, 5, 'C', 5, 'C', 2), 3) self.assertEqual(solve(5, 1, 5, 'A', 1, 'C', 5), 2) self.assertEqual(solve(10, 100, 1000, 'A', 1, 'C', 5), 5) self.assertEqual(solve(10, 100, 1000, 'A', 1, 'C', 995), 15) self.assertEqual(solve(10, 1, 1000, 'A', 10, 'C', 5), 6) self.assertEqual(solve(10, 1, 1000, 'A', 1, 'C', 995), 7) if __name__ == "__main__": # unittest.main() main()