x, y, z = map(int, input().split()) s0, t0 = input().split() s1, t1 = input().split() t0 = int(t0) t1 = int(t1) if s0 == s1: print(abs(t1 - t0)) else: candidates = [] # 情况1: A1 -> B1 a1_start = 0 if s0 == 'A': a1_start = t0 - 1 elif s0 == 'B': a1_start = (t0 - 1) + 1 elif s0 == 'C': a1_start = (t0 - 1) + 1 + 1 b1_end = 0 if s1 == 'B': b1_end = t1 - 1 elif s1 == 'A': b1_end = (t1 - 1) + 1 elif s1 == 'C': b1_end = (t1 - 1) + 1 + 1 case1 = a1_start + 1 + b1_end candidates.append(case1) # 情况2: B1 -> Cz b1_start = 0 if s0 == 'B': b1_start = t0 - 1 elif s0 == 'A': b1_start = (t0 - 1) + 1 elif s0 == 'C': b1_start = (t0 - 1) + 1 + 1 cz_end = 0 if s1 == 'C': cz_end = z - t1 elif s1 == 'B': cz_end = (y - t1) + 1 elif s1 == 'A': cz_end = (x - t1) + 2 case2 = b1_start + 1 + cz_end candidates.append(case2) # 情况3: Cz -> A1 cz_start = 0 if s0 == 'C': cz_start = z - t0 elif s0 == 'B': cz_start = (y - t0) + 1 elif s0 == 'A': cz_start = (x - t0) + 2 a1_end = 0 if s1 == 'A': a1_end = t1 - 1 elif s1 == 'B': a1_end = (t1 - 1) + 1 elif s1 == 'C': a1_end = (t1 - 1) + 1 + 1 case3 = cz_start + 1 + a1_end candidates.append(case3) # 情况4: Ax -> Cz ax_start = 0 if s0 == 'A': ax_start = x - t0 elif s0 == 'B': ax_start = (y - t0) + 1 + 1 elif s0 == 'C': ax_start = (z - t0) + 1 + 1 cz_end4 = 0 if s1 == 'C': cz_end4 = z - t1 elif s1 == 'B': cz_end4 = (y - t1) + 1 elif s1 == 'A': cz_end4 = (x - t1) + 2 case4 = ax_start + 1 + cz_end4 candidates.append(case4) # 情况5: By -> Cz by_start = 0 if s0 == 'B': by_start = y - t0 elif s0 == 'A': by_start = (x - t0) + 1 + 1 elif s0 == 'C': by_start = (z - t0) + 1 + 1 cz_end5 = 0 if s1 == 'C': cz_end5 = z - t1 elif s1 == 'B': cz_end5 = (y - t1) + 1 elif s1 == 'A': cz_end5 = (x - t1) + 2 case5 = by_start + 1 + cz_end5 candidates.append(case5) # 情况6: Ax -> By ax_start6 = 0 if s0 == 'A': ax_start6 = x - t0 elif s0 == 'B': ax_start6 = (y - t0) + 1 + 1 elif s0 == 'C': ax_start6 = (z - t0) + 1 + 1 by_end6 = 0 if s1 == 'B': by_end6 = y - t1 elif s1 == 'A': by_end6 = (x - t1) + 1 + 1 elif s1 == 'C': by_end6 = (z - t1) + 1 + 1 case6 = ax_start6 + 1 + by_end6 candidates.append(case6) min_price = min(candidates) print(min_price)