x, y, z = map(int, input().split()) S = input().split() G = input().split() p = int(S[1]) q = int(G[1]) P = 0 Q = 0 R = 0 F = 0 if S[0] == 'A' and G[0] == 'A': P = x - p Q = x - q R = min(y, z) elif S[0] == 'A' and G[0] == 'B': P = x - p Q = y - q R = z elif S[0] == 'A' and G[0] == 'C': P = x - p Q = z - q R = y elif S[0] == 'B' and G[0] == 'A': P = y - p Q = x - q R = z elif S[0] == 'B' and G[0] == 'B': P = y - p Q = y - q R = min(x,z) elif S[0] == 'B' and G[0] == 'C': P = y - p Q = z - q R = x elif S[0] == 'C' and G[0] == 'A': P = z - p Q = x - q R = y elif S[0] == 'C' and G[0] == 'B': P = z - p Q = y - q R = x elif S[0] == 'C' and G[0] == 'C': P = z - p Q = z - q R = min(x, y) if S[0] == G[0]: if p <= q: F = min(q-p, p+R+Q) else: F = min(p-q, P+R+q) else: F = abs(min(p+q-1, P+Q+1, p+R+Q, P+R+q)) print(F)