#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ ll x, y, z; scanf("%lld %lld %lld", &x, &y, &z); string s0, s1; ll t0, t1; cin >> s0 >> t0 >> s1 >> t1; ll res = INF; if (s0 == s1) { if (s0 == "B") { swap(x, y); } if (s0 == "C") { swap(x, z); } if (t0 > t1) swap(t0, t1); chmin(res, t1 - t0); chmin(res, x + y - (t1 - t0)); chmin(res, x + z - (t1 - t0)); } else { if (s0 == "A" && s1 == "C") swap(y, z); if (s0 == "B" && s1 == "C") swap(x, z), swap(x, y); if (s0 == "B" && s1 == "A") swap(x, y); if (s0 == "C" && s1 == "B") swap(x, z); if (s0 == "C" && s1 == "A") swap(y, z), swap(x, y); chmin(res, t0 + t1 - 1); //cout << res << endl; chmin(res, x + y - (t0 + t1 - 1)); //cout << res << endl; chmin(res, x - t0 + z + t1); //cout << res << endl; chmin(res, t0 + z + y - t1); //cout << res << endl; } cout << res << endl; return 0; }