#include #include using namespace std; typedef long long ll; int main(){ ll a, b, c; cin >> a >> b >> c; char sc, tc; ll si, ti; cin >> sc >> si >> tc >> ti; ll ans = 1ll<<60; ll x, y, z, w; x = si, z = ti; if(sc == 'A') y = a-si+1; if(sc == 'B') y = b-si+1; if(sc == 'C') y = c-si+1; if(tc == 'A') w = a-ti+1; if(tc == 'B') w = b-ti+1; if(tc == 'C') w = c-ti+1; if(sc == tc) ans = abs(ti-si); ans = min(ans, x + z-1); ans = min(ans, y + w-1); if(sc != 'A' && tc != 'A') ans = min({ans, x+a-1+w-1, y+a-1+z-1}); if(sc != 'B' && tc != 'B') ans = min({ans, x+b-1+w-1, y+b-1+z-1}); if(sc != 'C' && tc != 'C') ans = min({ans, x+c-1+w-1, y+c-1+z-1}); cout << ans << endl; return 0; }