#include using namespace std; typedef long long ll; int no(int a,int b){ if((a==0&&b==1)||(a==1&&b==0))return 2; if((a==0&&b==2)||(a==2&&b==0))return 1; return 0; } int main(){ ll xyz[3],t0,i,t1,ml=100000000000000,n0,n1,outl,gf,ge; char s0,s1; cin >> xyz[0] >> xyz[1] >> xyz[2] >> s0>>t0>>s1>>t1; n0=s0-'A'; n1=s1-'A'; if(n0==n1){ outl = min(t0,xyz[n0]-t0)+min(t1,xyz[n1]-t1)-2; ml = min(abs(t1-t0),outl+min(xyz[(n0+1)%3],xyz[(n0+2)%3])); }else{ i = no(n0,n1); gf = min(t0-1,xyz[n0]-t0+xyz[i]); ge = min(xyz[n0]-t0-1,t0+xyz[i]); ml = min(gf+t1,ge+xyz[n1]-t1); } cout << ml<