x, y, z = map(int, input().split()) S_line, S_num = input().split() S_num = int(S_num) T_line, T_num = input().split() T_num = int(T_num) if S_line == T_line: print(abs(S_num - T_num)) else: # Determine s_last and t_last s_last = x if S_line == 'A' else y if S_line == 'B' else z t_last = x if T_line == 'A' else y if T_line == 'B' else z # Option 1: via first stations cost1 = (S_num - 1) + 1 + (T_num - 1) # Option 2: via last stations cost2 = (s_last - S_num) + 1 + (t_last - T_num) # Option 3: via next line's first and last next_line = {'A': 'B', 'B': 'C', 'C': 'A'}[S_line] if next_line == 'A': next_first, next_last = 1, x elif next_line == 'B': next_first, next_last = 1, y else: next_first, next_last = 1, z cost3 = (S_num - 1) + 1 + (next_last - next_first) + 1 + (t_last - T_num) # Option 4: via previous line's last and first prev_line = {'A': 'C', 'B': 'A', 'C': 'B'}[S_line] if prev_line == 'A': prev_first, prev_last = 1, x elif prev_line == 'B': prev_first, prev_last = 1, y else: prev_first, prev_last = 1, z cost4 = (s_last - S_num) + 1 + (prev_last - prev_first) + 1 + (T_num - 1) min_cost = min(cost1, cost2, cost3, cost4) print(min_cost)