/* -*- coding: utf-8 -*- * * 859.cc: No.859 路線A、路線B、路線C - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int N = 3; /* typedef */ typedef long long ll; /* global variables */ /* subroutines */ inline void setmin(ll &a, ll b) { if (a > b) a = b; } /* main */ int main() { ll ls[N]; for (int i = 0; i < N; i++) scanf("%lld", ls + i), ls[i]--; char s0[4], s1[4]; ll t0, t1; scanf("%s%lld%s%lld", s0, &t0, s1, &t1); int r0 = s0[0] - 'A', r1 = s1[0] - 'A'; t0--, t1--; ll ans = 0; if (r0 == r1) { int q0 = (r0 + 1) % N, q1 = (r0 + 2) % N; if (t0 > t1) swap(t0, t1); ans = t1 - t0; setmin(ans, t0 + 1 + min(ls[q0], ls[q1]) + 1 + (ls[r0] - t1)); } else { int q0; for (q0 = 0; q0 == r0 || q0 == r1; q0++); ans = t0 + 1 + t1; setmin(ans, (ls[r0] - t0) + 1 + (ls[r1] - t1)); setmin(ans, t0 + 1 + ls[q0] + 1 + (ls[r1] - t1)); setmin(ans, (ls[r0] - t0) + 1 + ls[q0] + 1 + t1); } printf("%lld\n", ans); return 0; }