#include //#include using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long x, y, z; cin >> x >> y >> z; char s0, s1; int t0, t1; cin >> s0 >> t0; t0--; cin >> s1 >> t1; t1--; vector d(8, vector(8, LINF)); rep(i, 8)d[i][i] = 0; rep(i, 3)rep(j, 3)if (i != j) { d[i][j] = 1; d[i + 3][j + 3] = 1; } d[0][3] = x - 1; d[3][0] = x - 1; d[1][4] = y - 1; d[4][1] = y - 1; d[2][5] = z - 1; d[5][2] = z - 1; if ('A' == s0) { d[0][6] = t0; d[6][0] = t0; d[3][6] = x - 1 - t0; d[6][3] = x - 1 - t0; } else if ('B' == s0) { d[1][6] = t0; d[6][1] = t0; d[4][6] = y - 1 - t0; d[6][4] = y - 1 - t0; } else { d[2][6] = t0; d[6][2] = t0; d[5][6] = z - 1 - t0; d[6][5] = z - 1 - t0; } if ('A' == s1) { d[0][7] = t1; d[7][0] = t1; d[3][7] = x - 1 - t1; d[6][7] = x - 1 - t1; } else if ('B' == s1) { d[1][7] = t1; d[7][1] = t1; d[4][7] = y - 1 - t1; d[7][4] = y - 1 - t1; } else { d[2][7] = t1; d[7][2] = t1; d[5][7] = z - 1 - t1; d[7][5] = z - 1 - t1; } // warshall_floyd rep(k, 8) rep(i, 8)rep(j, 8) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } long long ans = d[6][7]; if (s0 == s1)chmin(ans, abs(t1 - t0)); cout << ans << endl; return 0; }