//#pragma GCC optimize ("-O3","unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i, n) for(int i = 0;i < n;i++) #define REPR(i, n) for(int i = n;i >= 0;i--) #define FOR(i, m, n) for(int i = m;i < n;i++) #define FORR(i, m, n) for(int i = m;i >= n;i--) #define SORT(v, n) sort(v, v+n); #define VSORT(v) sort(v.begin(), v.end()); #define REVERSE(v,n) reverse(v,v+n); #define VREVERSE(v) reverse(v.begin(), v.end()); #define ll long long #define pb(a) push_back(a) #define m0(x) memset(x,0,sizeof(x)) #define print(x) cout< inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const int MOD = 1e9 + 7; const ll INF = 1e17; const double pi = acos(-1); const double EPS = 1e-10; typedef pairP; const int MAX = 200020; signed main() { cin.tie(0); ios::sync_with_stdio(false); int x, y, z; cin >> x >> y >> z; char S[2]; int t[2]; cin >> S[0] >> t[0]; cin >> S[1] >> t[1]; int A1, Ax, B1, Bx, C1, Cx; if (S[0] == 'A') { if (S[1] == 'A') { int res1 = t[0] - 1; int res2 = x - t[0]; int d = abs(t[1] - t[0]); int l1 = res1 + z + x-t[1] + 1, l2 = res1 + y + x-t[1] + 1; print(min({ d, l1, l2 })); return 0; } else if (S[1] == 'B') { int res1 = t[0] - 1; int res2 = x - t[0]; int l1 = res1 + t[1], l2 = res1 + z + (y - t[1] + 1); int r1 = res2 + (y - t[1] + 1), r2 = res2 + z + t[1]; print(min({ l1,l2,r1,r2 })); return 0; } else { int res1 = t[0] - 1; int res2 = x - t[0]; int l1 = res1 + t[1], l2 = res1 + y + (z - t[1] + 1); int r1 = res2 + (z - t[1] + 1), r2 = res2 + y + t[1]; print(min({ l1,l2,r1,r2 })); } } else if (S[0] == 'B') { int res1 = t[0] - 1; int res2 = y - t[0]; if (S[1] == 'B') { int d = abs(t[0] - t[1]); int l1 = res1 + x + y-t[1] + 1, l2 = res1 + y + y-t[1] + 1; int r1 = res2 + x + t[1]-1 + 1, r2 = res2 + z + t[1]-1 + 1; print(min({ d,r1,r2,l1,l2 })); } else if (S[1] == 'A') { int l1 = res1 + t[1], l2 = res1 + z +x- t[1] + 1; int r1 = res2 + x - t[1] + 1, r2 = res2 + z + x - t[1] + 1; print(min({ l1,l2,r1,r2 })); } else { int l1 = res1 + t[1], l2 = res1 + z + x - t[1] + 1; int r1 = res2 + z - t[1] + 1, r2 = res2 + z + x - t[1] + 1; print(min({ l1,l2,r1,r2 })); } } else { if (S[1] == 'C') { int res1 = t[0] - 1; int res2 = z - t[0]; int d = abs(t[1] - t[0]); int l1 = res1 + x + z-t[1] + 1, l2 = res1 + y + z-t[1] + 1; print(min({ d, l1, l2 })); return 0; } else if (S[1] == 'B') { int res1 = t[0] - 1; int res2 = z- t[0]; int l1 = res1 + t[1], l2 = res1 + x + (y - t[1] + 1); int r1 = res2 + (y - t[1] + 1), r2 = res2 + x + t[1]; print(min({ l1,l2,r1,r2 })); return 0; } else { int res1 = t[0] - 1; int res2 = z - t[0]; int l1 = res1 + t[1], l2 = res1 + y + (x - t[1] + 1); int r1 = res2 + (x - t[1] + 1), r2 = res2 + y + t[1]; print(min({ l1,l2,r1,r2 })); } } }