INF = 10 ** 7 import sys input = sys.stdin.readline sys.setrecursionlimit(100000000) dy = (-1,0,1,0) dx = (0,1,0,-1) def main(): x,y,z = map(int,input().split()) s = input().split() g = input().split() ss = s[0] si = int(s[1]) gs = g[0] gi = int(g[1]) if ss == 'A' and gs == 'B': ans = min(si + gi - 1,si + z + y - gi,x - si + y - gi + 1,x - si + z + gi) elif ss == 'A' and gs == 'C': ans = min(si + gi - 1,si + y + z - gi,x - si + z - gi + 1,x - si + y + gi) elif ss == 'A' and gs == 'A': if si >= gi: si,gi = gi,si ans = min(gi - si,si + min(y,z) + x - gi) elif ss == 'B' and gs == 'C': ans = min(si + gi - 1,si + x + z - gi,y - si + z - gi + 1,y - si + x + gi) elif ss == 'B' and gs == 'A': ans = min(si + gi - 1,si + z + x - gi,y - si + x - gi + 1,y - si + z + gi) elif ss == 'B' and gs == 'B': if si >= gi: si,gi = gi,si ans = min(gi - si,si + min(x,z) + y - gi) elif ss == 'C' and gs == 'A': ans = min(si + gi - 1,si + x + y - gi,z - si + x - gi + 1,z - si + y + gi) elif ss == 'C' and gs == 'B': ans = min(si + gi - 1,si + y + x - gi,z - si + y - gi + 1,z - si + x + gi) elif ss == 'C' and gs == 'C': if si >= gi: si,gi = gi,si ans = min(gi - si,si + min(x,y) + z - gi) print(ans) if __name__ == '__main__': main()