#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int x, y, z; char s0; int t0; char s1; int t1; //--------------------------------------------------------------------------------------------------- map ma; map> E; string enc(char c, int i) { return to_string(i) + c; } void add(char c1, int i1, char c2, char i2, int len) { if (c1 == c2 and i1 == i2) return; E[enc(c1, i1)][enc(c2, i2)] = len; E[enc(c2, i2)][enc(c1, i1)] = len; } void _main() { cin >> x >> y >> z; cin >> s0 >> t0 >> s1 >> t1; ma['A'] = x; ma['B'] = y; ma['C'] = z; add('A', 1, 'B', 1, 1); add('A', 1, 'C', 1, 1); add('B', 1, 'C', 1, 1); add('A', x, 'B', y, 1); add('A', x, 'C', z, 1); add('B', y, 'C', z, 1); add('A', 1, 'A', x, x - 1); add('B', 1, 'B', y, y - 1); add('C', 1, 'C', z, z - 1); add(s0, 1, s0, t0, t0 - 1); add(s0, ma[s0], s0, t0, ma[s0] - t0); add(s1, 1, s1, t1, t1 - 1); add(s1, ma[s1], s1, t1, ma[s1] - t1); if (s0 == s1) add(s0, t0, s1, t1, abs(t0 - t1)); vector nodes; fore(p, E) nodes.push_back(p.first); map> d; fore(from, nodes) fore(to, nodes) d[from][to] = infl; fore(node, nodes) d[node][node] = 0; fore(cu, nodes) fore(p, E[cu]) d[cu][p.first] = p.second; fore(k, nodes) fore(i, nodes) fore(j, nodes) chmin(d[i][j], d[i][k] + d[k][j]); ll ans = d[enc(s0, t0)][enc(s1, t1)]; cout << ans << endl; }