#include using namespace std; using ll = long long; template using vt = vector; template using vvt = vector>; template using ttt = tuple; using tii = tuple; using tiii = tuple; using vi = vector; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define pb push_back #define mt make_tuple #define ALL(a) (a).begin(),(a).end() #define FST first #define SEC second #define DEB cerr<<"!"<0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;} inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;} #define chmax(a,b) a=max(a,b) /*Coding Space*/ int main(){ ll a,b,c; cin >> a >> b >> c; char s; ll sn; cin >> s >> sn; char g; ll gn; cin >> g >> gn; ll ans = LLINF; if(s != 'B' && g != 'B'){ if(s == g){ ll manu = max(sn,gn); ll minu = min(sn,gn); if(s == 'C') swap(a,c); ans = min(manu - minu, a - manu + b + minu); }else{ ans = min(sn+gn-1,ans); if(s == 'C') swap(a,c); ans = min(ans, a-sn+c-gn+1); ans = min(ans, a-sn+b+gn); ans = min(ans, sn+b+c-gn); } }else{ if(s == g){ ll manu = max(sn,gn); ll minu = min(sn,gn); ans = min(ans, manu - minu); ans = min(ans, minu + a + b - manu); ans = min(ans, minu + c + b - manu); }else{ if(s != 'B') swap(sn,gn); if(g == 'C') swap(a,c); ans = min(sn + gn-1,ans); ans = min(b-sn + a - gn+1,ans); ans = min(sn+c+a-gn,ans); ans = min(gn+c+b-sn,ans); } } cout << ans << endl; }