#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define sz size() #define pb push_back #define mp make_pair #define fi first #define se second #define all(c) (c).begin(), (c).end() #define rep(i,a,b) for(ll i=(a);i<(b);++i) #define per(i,a,b) for(ll i=b-1LL;i>=(a);--i) #define clr(a, b) memset((a), (b) ,sizeof(a)) #define ctos(c) string(1,c) #define print(x) cout<<#x<<" = "<>d[0]>>d[1]>>d[2]; char s[2]; ll t[2]; cin>>s[0]>>t[0]; cin>>s[1]>>t[1]; vector > vp; vp.pb(mp(s[0]-'A',t[0]-1)); vp.pb(mp(s[1]-'A',t[1]-1)); if(vp[0].se>vp[1].se)swap(vp[0],vp[1]); if(vp[0].fi==vp[1].fi){ ll x = d[vp[0].fi]; ll y = d[(vp[0].fi+1)%3]; ll z = d[(vp[0].fi+2)%3]; cout << min(vp[1].se-vp[0].se,vp[0].se+x-vp[1].se+min(y,z)) << endl; } else{ ll x = d[vp[0].fi]; ll y = d[vp[1].fi]; ll z = d[3-vp[0].fi-vp[1].fi]; ll ans1 = min(vp[0].se+vp[1].se+1,x-vp[0].se+y-vp[1].se-1); ll ans2 = min(vp[0].se+z+y-vp[1].se,x-vp[0].se+z+vp[1].se); cout << min(ans1,ans2) << endl; } return 0; }