#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) > (b) ? (b) : (a))
#define abs(x) ((x) > 0 ? (x) : -(x))
#define rep(i, n) for(int i = 0; i < (n); i++)
#define MOD 1000000007 //10^9 + 7
#define endl printf("\n")
typedef long long ll;
#define MAX_N (1 << 17)

void
swap(int* a, int* b){
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
  return;
}

int
main(int argc, char *argv[])
{
  int x, y, z;
  scanf("%d %d %d\n", &x, &y, &z);
  char s[2], ctmp; int t[2], tmp;
  
  
  scanf("%c", &s[0]);
  scanf("%d\n", &t[0]);
  scanf("%c", &s[1]);
  scanf("%d", &t[1]);
  
  if (s[0] > s[1]) {
    ctmp = s[0];
    tmp = t[0];
    s[0] = s[1];
    t[0] = t[1];
    s[1] = ctmp;
    t[1] = tmp;
  }

  int a, b, c, d, e, f; int ans;
  if (s[0] == s[1]) {
    int m = Min(t[0], t[1]);
    int M = Max(t[0], t[1]);
    a = M - m;
    if (s[0] == 'A') {
      b = m + M - x + y;
      c = m + M - x + z;
      ans = Min(a, Min(b, c));
    }
    if (s[0] == 'B') {
      b = m + M - y + x;
      c = m + M - y + z;
      ans = Min(a, Min(b, c));
    }
    if (s[0] == 'C') {
      b = m + M - z + x;
      c = m + M - z + y;
      ans = Min(a, Min(b, c));
    }
  } else {
    if (s[1] == 'B') swap(&y, &z);
    if (s[0] == 'B') {
      int temp = x;
      x = y; y = z; z = temp;
    }

    
    a = t[0] + t[1] - 1;
    b = x - t[0] + 1 + z - t[1];
    c = t[0] - 1 + y + z - t[1] + 1;
    d = x - t[0] + y + t[1];

    ans = Min(a, Min(b, Min(c, d)));
  }

  printf("%d\n", ans);

  return 0;
}