#include using namespace std; using ll = long long; using pii = pair; using vi = vector; using vl = vector; #define rep(i, n) for(ll i = 0;i < n;i++) #define all(i) i.begin(), i.end() template bool cmax(T& a, U b) { if (a bool cmin(T& a, U b) { if (a>b) {a = b; return true;} else return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); vector d(3); rep(i, 3) { cin >> d[i]; } char a, b; int s, g; cin >> a >> s >> b >> g; int ss = a-'A'; int gg = b-'A'; ll ans = 1e10; if (ss == gg) { cmin(ans, abs(s-g)); ll m = 1e10; rep(i, 3) { if (i != ss) cmin(m, d[i]); } cmin(ans, min(s, g) + d[ss] - max(s, g) + m); } else { cmin(ans, s+g-1); cmin(ans, d[ss]-s+d[ss]-g+1); int m; rep(i, 3) if (i != ss && i != gg) m = i; cmin(ans, s+d[m]+d[gg]-g); cmin(ans, d[ss]-s+d[m]+g); } cout << ans << endl; }