#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; // g <- pair < v , cost > template < class T > vector< T > dijkstra(vector>> &graph, int s) { T INF = numeric_limits< T >::max(); vector dist(graph.size(), INF); priority_queue, vector>, greater>> q; q.push({dist[s] = T(0), s}); while(!q.empty()){ auto [uc, ui] = q.top(); q.pop(); if(uc != dist[ui]) continue; for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) q.push({dist[vi] = uc + vc, vi}); } return dist; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int x,y,z; cin >> x >> y >> z; char s0, s1; int t0, t1; cin >> s0 >> t0; cin >> s1 >> t1; int N = 8; vector>> G(N); int A1 = 0, B1 = 1, C1 = 2; int Ax = 3, By = 4, Cz = 5; G[A1].push_back({B1, 1}); G[B1].push_back({A1, 1}); G[B1].push_back({C1, 1}); G[C1].push_back({B1, 1}); G[C1].push_back({A1, 1}); G[A1].push_back({C1, 1}); G[Ax].push_back({By, 1}); G[By].push_back({Ax, 1}); G[By].push_back({Cz, 1}); G[Cz].push_back({By, 1}); G[Cz].push_back({Ax, 1}); G[Ax].push_back({Cz, 1}); G[A1].push_back({Ax, x - 1}); G[Ax].push_back({A1, x - 1}); G[B1].push_back({By, y - 1}); G[By].push_back({B1, y - 1}); G[C1].push_back({Cz, z - 1}); G[Cz].push_back({C1, z - 1}); auto f = [&](char s, int t, int b) { if(s == 'A' && t == 1) return A1; if(s == 'A' && t == x) return Ax; if(s == 'B' && t == 1) return B1; if(s == 'B' && t == y) return By; if(s == 'C' && t == 1) return C1; if(s == 'C' && t == z) return Cz; int ID = 6 + b; if(s == 'A') { G[ID].push_back({A1, t - 1}); G[A1].push_back({ID, t - 1}); G[ID].push_back({Ax, x - t}); G[Ax].push_back({ID, x - t}); } if(s == 'B') { G[ID].push_back({B1, t - 1}); G[B1].push_back({ID, t - 1}); G[ID].push_back({By, y - t}); G[By].push_back({ID, y - t}); } if(s == 'C') { G[ID].push_back({C1, t - 1}); G[C1].push_back({ID, t - 1}); G[ID].push_back({Cz, z - t}); G[Cz].push_back({ID, z - t}); } return ID; }; int S = f(s0, t0, 0); int T = f(s1, t1, 1); cout << dijkstra(G, S)[T] << endl; }