from heapq import * x, y, z = map(int, input().split()) s0,t0 = map(str, input().split()) s1,t1 = map(str, input().split()) INF = float('inf') v = [[s0,t0],[s1,t1]] def dijkstra(s, n): dist = [INF] * n hq = [(0, s)] dist[s] = 0 seen = [False] * n # 経路復元用 # prev = [-1]*n while hq: dis, v = heappop(hq) if dist[v] < dis: continue seen[v] = True for to, cost in G[v]: if seen[to] == False and dist[v] + cost < dist[to]: dist[to] = dist[v] + cost # prev[to] = v heappush(hq, (dist[to], to)) return dist G = [list() for _ in range(4)] for a in (x, y, z): G[0].append((1, a)) G[1].append((0, a)) cnt = 2 ans = float('inf') if s0!=s1 else abs(int(t0)-int(t1)) for b, c in v: if b == 'A': res = x elif b == 'B': res = y else: res = z G[cnt].append((0, int(c)-0.5)) G[cnt].append((1%2, res-int(c)+0.5)) G[0].append((cnt, int(c)-0.5)) G[1].append((cnt, res-int(c)+0.5)) cnt += 1 d = dijkstra(2,4) print(min(ans,int(d[3])))