#include using namespace std; using lint = long long int; using pint = pair; using plint = pair; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define eprintf(...) 42 #endif lint abc_to_xyz(lint x,lint y,lint z,string abc){ if(abc=="A") return x; if (abc == "B") return y; if (abc == "C") return z; } int main() { lint x, y,z; cin >> x >> y >> z; lint ans, tmp; string s0, s1; lint t0, t1; cin >> s0 >> t0; cin >> s1 >> t1; lint i = t0; lint j = t1; lint s = abc_to_xyz(x, y, z, s0); //スタート路線の長さ lint g = abc_to_xyz(x, y, z, s1); //ゴール路線の長さ lint l = i - 1, r = s - i; lint goleft = min(l, r + min(min(x,y),z)); lint goright = min(r, l + min(min(x, y), z)); ans = min(goleft + j, goright + g - j + 1); if (s0 == s1) { ans = abs(j - i); } cout << ans << "\n"; return 0; }