#!/usr/bin/env python3 x, y, z = map(int, input().split()) mp = {'A': x, 'B': y, 'C': z} s0, t0 = input().split() s1, t1 = input().split() t0 = int(t0) t1 = int(t1) ans = 10**9 if s0 == s1: s1 = '' s2 = '' for i in range(3): if 'ABC'[i] == s0: continue s1 = 'ABC'[i] for i in range(3): if 'ABC'[i] == s0: continue if 'ABC'[i] == s1: continue s2 = 'ABC'[i] ans = min(ans, abs(t1 - t0)) ans = min(ans, t0 + (mp[s0] - t1) + mp[s1]) ans = min(ans, t0 + (mp[s0] - t1) + mp[s2]) else: s2 = '' for i in range(3): if 'ABC'[i] == s0: continue if 'ABC'[i] == s1: continue s2 = 'ABC'[i] ans = min(ans, t0 + t1 - 1) ans = min(ans, t0 + (mp[s1] - t1) + mp[s2]) ans = min(ans, (mp[s0] - t0) + t1 + mp[s2]) ans = min(ans, (mp[s0] - t0) + (mp[s1] - t1) + 1) print(ans)