#include using namespace std; using ll = long long; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vs = vector; using pll = pair; using vp = vector; #define rep(i, n) for(ll i = 0; i < (n); i++) #define repr(i, a, b) for(ll i = (a); i < (b); i++) #define ALL(a) (a).begin(), (a).end() #define SZ(x) ((ll)(x).size()) const ll MOD = 1000000007; const ll INF = 100000000000000000LL; inline ll GCD(ll a, ll b){ return b?GCD(b, a % b):a; } inline ll LCM(ll a, ll b){ return a/GCD(a, b)*b; } inline ll powint(ll x, ll y){ ll r=1; while(y){ if(y&1) r*=x; x*=x; y>>=1; } return r; } inline ll powmod(ll x, ll y, ll m = MOD){ ll r=1; while(y){ if(y&1) r*=x; x*=x; r%=m; x%=m; y>>=1; } return r; } templatebool chmax(T &a, const T &b){ if(abool chmin(T &a, const T &b){ if(b > AdjList; AdjList graph; typedef pair P; vector dist; void dijkstra(ll n, ll s){ dist = vector(n,INF); dist[s] = 0; priority_queue, greater

> que; que.push(P(0,s)); while(!que.empty()){ P p = que.top(); que.pop(); ll v = p.second; if(dist[v] < p.first){ continue; } for(ll i=0;i dist[v] + e.cost){ dist[e.to] = dist[v] + e.cost; que.push(P(dist[e.to],e.to)); } } } } int main(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); ll x, y, z; cin >> x >> y >> z; char s1, s2; ll t1, t2; cin >> s1 >> t1 >> s2 >> t2; s1 -= 'A'; s2 -= 'A'; graph = AdjList(8); rep(i, 3)repr(j, i+1, 3){ if(i != j) graph[i].emplace_back(j, 1); if(i != j) graph[j].emplace_back(i, 1); } repr(i, 3, 6)repr(j, i+1, 6){ if(i != j) graph[i].emplace_back(j, 1); if(i != j) graph[j].emplace_back(i, 1); } if(s1 == s2){ vll r{x, y, z}; if(t1 > t2) swap(t1, t2); rep(i, 3){ if(i == s1){ graph[i].emplace_back(6, t1-1); graph[6].emplace_back(i, t1-1); graph[6].emplace_back(7, t2-t1); graph[7].emplace_back(6, t2-t1); graph[7].emplace_back(i+3, r[i]-t2); graph[i+3].emplace_back(7, r[i]-t2); }else{ graph[i].emplace_back(i+3, r[i]-1); graph[i+3].emplace_back(i, r[i]-1); } } }else{ vll r{x, y, z}; rep(i, 3){ if(i == s1){ graph[i].emplace_back(6, t1-1); graph[6].emplace_back(i, t1-1); graph[6].emplace_back(i+3, r[i]-t1); graph[i+3].emplace_back(6, r[i]-t1); }else if(i == s2){ graph[i].emplace_back(7, t2-1); graph[7].emplace_back(i, t2-1); graph[7].emplace_back(i+3, r[i]-t2); graph[i+3].emplace_back(7, r[i]-t2); }else{ graph[i].emplace_back(i+3, r[i]-1); graph[i+3].emplace_back(i, r[i]-1); } } } rep(i, 8)rep(j, SZ(graph[i])){ dump(i, j, graph[i][j].to, graph[i][j].cost); } dijkstra(8, 6); dump(dist); cout << dist[7] << endl; return 0; }