#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cstdint>
#include <cassert>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

i64 xs[3];
i64 s0, t0, s1, t1;
int main() {
  read_n(xs, 3);
  char c0, c1;
  cin >> c0 >> t0 >> c1 >> t1;
  s0 = c0 - 'A';
  s1 = c1 - 'A'; 
  if (t0 > t1) {
    swap(s0, s1);
    swap(t0, t1);
  }
  i64 ans;
  if (s0 == s1) {
    if (s0 != 0) {
      swap(xs[0], xs[s0]);
    }
    ans = min(t1 - t0, t0 + min(xs[1], xs[2]) + xs[0] - t1);
  } else {
    if (s0 != 0) {
      swap(xs[0], xs[s0]);
      if (s1 == 0) s1 = s0;
    }
    if (s1 != 1) {
      swap(xs[1], xs[s1]);
    }
    ans = min({
        t0 + t1 - 1,
        xs[0] - t0 + xs[1] - t1 + 1,
        t0 + xs[2] + xs[1] - t1,
        t1 + xs[2] + xs[0] - t0,
    });
  }

  cout << ans << endl;
  return 0;
}