x, y, z = map(int, input().split()) S0, t0 = input().split() t0 = int(t0) S1, t1 = input().split() t1 = int(t1) if S0 == S1: print(abs(t0 - t1)) else: routes = {'A': (1, x), 'B': (1, y), 'C': (1, z)} S = S0 T = S1 S_start, S_end = routes[S] T_start, T_end = routes[T] # Compute the four possible costs cost1 = abs(t0 - S_start) + 1 + abs(t1 - T_start) cost2 = abs(t0 - S_end) + 1 + abs(t1 - T_end) cost3 = abs(t0 - S_start) + 2 + abs(t1 - T_end) cost4 = abs(t0 - S_end) + 2 + abs(t1 - T_start) min_cost = min(cost1, cost2, cost3, cost4) print(min_cost)