#include #include #include #include #include #include #define Max(a, b) ((a) > (b) ? (a) : (b)) #define Min(a, b) ((a) > (b) ? (b) : (a)) #define abs(x) ((x) > 0 ? (x) : -(x)) #define rep(i, n) for(int i = 0; i < (n); i++) #define MOD 1000000007 //10^9 + 7 #define endl printf("\n") typedef long long ll; #define MAX_N (1 << 17) void swap(ll* a, ll* b){ int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main(int argc, char *argv[]) { ll x, y, z; scanf("%lld %lld %lld\n", &x, &y, &z); char s[2], ctmp; ll t[2], tmp; scanf("%c", &s[0]); scanf("%lld\n", &t[0]); scanf("%c", &s[1]); scanf("%lld", &t[1]); if (s[0] > s[1]) { ctmp = s[0]; tmp = t[0]; s[0] = s[1]; t[0] = t[1]; s[1] = ctmp; t[1] = tmp; } ll a, b, c, d, e, f; ll ans; if (s[0] == s[1]) { ll m = Min(t[0], t[1]); ll M = Max(t[0], t[1]); a = M - m; if (s[0] == 'A') { b = m + M - x + y; c = m + M - x + z; ans = Min(a, Min(b, c)); } if (s[0] == 'B') { b = m + M - y + x; c = m + M - y + z; ans = Min(a, Min(b, c)); } if (s[0] == 'C') { b = m + M - z + x; c = m + M - z + y; ans = Min(a, Min(b, c)); } } else { if (s[1] == 'B') swap(&y, &z); if (s[0] == 'B') { ll temp = x; x = y; y = z; z = temp; } a = t[0] + t[1] - 1; b = x - t[0] + 1 + z - t[1]; c = t[0] - 1 + y + z - t[1] + 1; d = x - t[0] + y + t[1]; ans = Min(a, Min(b, Min(c, d))); } printf("%lld\n", ans); return 0; }