#include #include #include using namespace std; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { vector c(3); for (long long &ci: c) cin >> ci; char s0, s1; long long t0, t1; cin >> s0 >> t0 >> s1 >> t1; s0 -= 'A', s1 -= 'A'; if (s0 == s1) { swap(c[0], c[s0]); if (t0 > t1) swap(t0, t1); long long ans = min(t1 - t0, t0 + min(c[1], c[2]) + c[0] - t1); cout << ans << endl; } else { long long x = c[s0], y = c[s1], z = c[6 - s0 - s1]; long long ans = min({ t0 + t1 - 1, x - t0 + y - t1 + 1, t0 + z + t1 - y, x - t0 + z + t1 }); cout << ans << endl; } return 0; }