#include using namespace std; int main() { int x, y, z; cin>>x>>y>>z; char s0, s1; long t0, t1; cin>>s0>>t0; cin>>s1>>t1; if (s0 > s1) { swap(s0, s1); swap(t0, t1); } else if (s0 == s1) { if (t0 > t1) { swap(t0, t1); } } long ans = x + y + z; if (s0 == 'A') { if (s1 == 'A') { ans = min(t1 - t0, t0 + y + x - t1); ans = min(t0 + z + x - t1, ans); } else if (s1 == 'B') { ans = min(t0 + t1 - 1, x - t0 + y - t1 + 1); ans = min(t0 + z + y - t1, ans); } else { ans = min(t0 + t1 - 1, x - t0 + z - t1 + 1); ans = min(t0 + y + z - t1, ans); } } else if (s0 == 'B') { if (s1 == 'B') { ans = min(t1 - t0, t0 + x + y - t1); ans = min(t0 + z + y - t1, ans); } else if (s1 == 'C') { ans = min(t0 + t1 - 1, y - t0 + z - t1 + 1); ans = min(t0 + x + z - t1, ans); } } else { ans = min(t1 - t0, t0 + x + z - t1); ans = min(t0 + y + z - t1, ans); } cout<