from heapq import * def dijkstra(g,start): n = len(g) INF = 1<<61 dist = [INF]*(n) #startからの最短距離 #pending = n-1 #未確定の点の個数 dist[start] = 0 q = [(0,start)] #(そこまでの距離、点) while q:# and pending: dv,v = heappop(q) if dist[v] < dv: continue for to, cost in g[v]: if dv + cost < dist[to]: dist[to] = dv + cost heappush(q, (dist[to], to)) return dist x,y,z = map(int, input().split()) s,a = input().split() t,b = input().split() g = [[] for _ in range(8)] for p,q,r in[(0,1,1),(1,2,1),(0,2,1),(3,4,1),(3,5,1),(4,5,1),(0,3,x-1),(2,5,z-1),(1,4,y-1)]: g[p].append((q,r)) g[q].append((p,r)) a = int(a) b = int(b) c = "ABC".find(s) g[6] = [(c,a-1),(c+3,[x,y,z][c]-a)] c = "ABC".find(t) g[c].append((7,b-1)) g[c+3].append((7,[x,y,z][c]-b)) dist = dijkstra(g,6) v = dist[7] if s==t: v = min(v,abs(a-b)) print(v)