// #define _GLIBCXX_DEBUG // for STL debug (optional) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long int; using int64 = long long int; template void chmax(T &a, T b) {a = max(a, b);} template void chmin(T &a, T b) {a = min(a, b);} template void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1LL << 60; const ll MOD = 1000000007LL; const int A1 = 0; const int AX = 1; const int B1 = 2; const int BY = 3; const int C1 = 4; const int CZ = 5; int main() { ll X, Y, Z; cin >> X >> Y >> Z; char S0, S1; ll T0, T1; cin >> S0 >> T0 >> S1 >> T1; ll sid, gid; // A1, Ax, C1, Cz, B1, By, S0, S1 だけ使う int N = 6; vector< tuple > edges; edges.emplace_back(A1, B1, 1); edges.emplace_back(A1, C1, 1); edges.emplace_back(B1, C1, 1); edges.emplace_back(A1, AX, X-1); edges.emplace_back(B1, BY, Y-1); edges.emplace_back(C1, CZ, Z-1); edges.emplace_back(AX, BY, 1); edges.emplace_back(AX, CZ, 1); edges.emplace_back(BY, CZ, 1); function add_vertex = [&](char c, ll k, bool sg) { if(c == 'A') { if(1 < k and k < X) { int id = N++; edges.emplace_back(A1, id, k - 1); edges.emplace_back(id, AX, X - k); (sg ? sid : gid) = id; } else if(k == 1) { (sg ? sid : gid) = A1; } else if(k == X) { (sg ? sid : gid) = AX; } else assert(false); } if(c == 'B') { if(1 < k and k < Y) { int id = N++; edges.emplace_back(B1, id, k - 1); edges.emplace_back(id, BY, Y - k); (sg ? sid : gid) = id; } else if(k == 1) { (sg ? sid : gid) = B1; } else if(k == Y) { (sg ? sid : gid) = BY; } else assert(false); } if(c == 'C') { if(1 < k and k < Z) { int id = N++; edges.emplace_back(C1, id, k - 1); edges.emplace_back(id, CZ, Z - k); (sg ? sid : gid) = id; } else if(k == 1) { (sg ? sid : gid) = C1; } else if(k == Z) { (sg ? sid : gid) = CZ; } else assert(false); } }; add_vertex(S0, T0, true); add_vertex(S1, T1, false); vector< vector > dist(N, vector(N, INF)); for(auto e : edges) { ll u, v, d; tie(u, v, d) = e; chmin(dist[u][v], d); chmin(dist[v][u], d); } for(int i=0; i