import math def solve(): # x, y, z are max station numbers, not directly used in cost calculation # as transfers are based on station 1 or direct target. x, y, z = map(int, input().split()) s0_line_char, s0_station_str = input().split() s1_line_char, s1_station_str = input().split() s0_station = int(s0_station_str) s1_station = int(s1_station_str) ans = float('inf') # Path 0: Direct travel on the same line if s0_line_char == s1_line_char: ans = min(ans, abs(s0_station - s1_station)) # Helper function for the cost of a single transfer segment. # Calculates cost from (l1, n1) to (l2, n2) assuming l1 != l2. def get_transfer_segment_cost(l1, n1, l2, n2): # Standard transfer (A-B, A-C): # Path: (l1, n1) -> l1_station_1 --(1 yen hop)--> l2_station_1 -> (l2, n2) if (l1 == 'A' and l2 == 'B') or \ (l1 == 'B' and l2 == 'A'): return abs(n1 - 1) + 1 + abs(1 - n2) if (l1 == 'A' and l2 == 'C') or \ (l1 == 'C' and l2 == 'A'): return abs(n1 - 1) + 1 + abs(1 - n2) # Special B-C transfer: # Path: (l1, n1) -> l1_station_1 --(1 yen hop)--> (l2, n2 directly) # Cost includes travel to station 1 on l1, and the 1 yen transfer hop. # Arrival is directly at station n2 on line l2. if (l1 == 'B' and l2 == 'C'): # From (B, n1) to (C, n2) return abs(n1 - 1) + 1 if (l1 == 'C' and l2 == 'B'): # From (C, n1) to (B, n2) return abs(n1 - 1) + 1 # Should not be reached if l1 != l2 for lines A, B, C. return float('inf') # Path 1: One transfer hop: s0_line -> s1_line if s0_line_char != s1_line_char: cost_path1 = get_transfer_segment_cost(s0_line_char, s0_station, s1_line_char, s1_station) ans = min(ans, cost_path1) # Path 2: Two transfer hops: s0_line -> inter_line -> s1_line # This path is relevant if s0_line is different from s1_line. if s0_line_char != s1_line_char: all_lines = ['A', 'B', 'C'] for inter_line_char in all_lines: # The intermediate line must be different from the start and end lines. if inter_line_char == s0_line_char or inter_line_char == s1_line_char: continue # Cost from (s0_line, s0_station) to (inter_line_char, 1) # The '1' signifies arriving at station 1 of the intermediate line. cost_to_intermediate = get_transfer_segment_cost(s0_line_char, s0_station, inter_line_char, 1) # Cost from (inter_line_char, 1) to (s1_line, s1_station) # Starting from station 1 of the intermediate line. cost_from_intermediate = get_transfer_segment_cost(inter_line_char, 1, s1_line_char, s1_station) if cost_to_intermediate != float('inf') and cost_from_intermediate != float('inf'): ans = min(ans, cost_to_intermediate + cost_from_intermediate) print(int(ans)) solve()