import java.io.*; import java.util.Scanner; public class Main_yukicoder859 { private static Scanner sc; private static Printer pr; private static void solve() { x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); char s0 = sc.next().toCharArray()[0]; int t0 = sc.nextInt(); char s1 = sc.next().toCharArray()[0]; int t1 = sc.nextInt(); long min = Long.MAX_VALUE; if (s0 == 'A' && s1 == 'A') { min = Math.min(min, Math.abs(t0 - t1)); min = Math.min(min, A1(t0) + 1 + y + Ax(t1)); min = Math.min(min, A1(t1) + 1 + y + Ax(t0)); min = Math.min(min, A1(t0) + 1 + z + Ax(t1)); min = Math.min(min, A1(t1) + 1 + z + Ax(t0)); } else if (s0 == 'B' && s1 == 'B') { min = Math.min(min, Math.abs(t0 - t1)); min = Math.min(min, B1(t0) + 1 + x + By(t1)); min = Math.min(min, B1(t1) + 1 + x + By(t0)); min = Math.min(min, B1(t0) + 1 + z + By(t1)); min = Math.min(min, B1(t1) + 1 + z + By(t0)); } else if (s0 == 'C' && s1 == 'C') { min = Math.min(min, Math.abs(t0 - t1)); min = Math.min(min, C1(t0) + 1 + x + Cz(t1)); min = Math.min(min, C1(t1) + 1 + x + Cz(t0)); min = Math.min(min, C1(t0) + 1 + y + Cz(t1)); min = Math.min(min, C1(t1) + 1 + y + Cz(t0)); } else if (s0 == 'A' && s1 == 'B') { min = Math.min(min, A1(t0) + 1 + B1(t1)); min = Math.min(min, A1(t0) + 1 + z + By(t1)); min = Math.min(min, Ax(t0) + 1 + By(t1)); min = Math.min(min, Ax(t0) + 1 + z + B1(t1)); } else if (s0 == 'A' && s1 == 'C') { min = Math.min(min, A1(t0) + 1 + C1(t1)); min = Math.min(min, A1(t0) + 1 + y + Cz(t1)); min = Math.min(min, Ax(t0) + 1 + Cz(t1)); min = Math.min(min, Ax(t0) + 1 + y + C1(t1)); } else if (s0 == 'B' && s1 == 'C') { min = Math.min(min, B1(t0) + 1 + C1(t1)); min = Math.min(min, B1(t0) + 1 + x + Cz(t1)); min = Math.min(min, By(t0) + 1 + Cz(t1)); min = Math.min(min, By(t0) + 1 + x + C1(t1)); } else if (s0 == 'B' && s1 == 'A') { min = Math.min(min, B1(t0) + 1 + A1(t1)); min = Math.min(min, B1(t0) + 1 + z + Ax(t1)); min = Math.min(min, By(t0) + 1 + Ax(t1)); min = Math.min(min, By(t0) + 1 + z + A1(t1)); } else if (s0 == 'C' && s1 == 'A') { min = Math.min(min, C1(t0) + 1 + A1(t1)); min = Math.min(min, C1(t0) + 1 + y + Ax(t1)); min = Math.min(min, Cz(t0) + 1 + Ax(t1)); min = Math.min(min, Cz(t0) + 1 + y + A1(t1)); } else if (s0 == 'C' && s1 == 'B') { min = Math.min(min, C1(t0) + 1 + B1(t1)); min = Math.min(min, C1(t0) + 1 + x + By(t1)); min = Math.min(min, Cz(t0) + 1 + By(t1)); min = Math.min(min, Cz(t0) + 1 + x + B1(t1)); } else { throw new RuntimeException(); } pr.println(min); } private static int x; private static int y; private static int z; private static long A1(int t) { return t - 1; } private static long Ax(int t) { return x - t; } private static long B1(int t) { return t - 1; } private static long By(int t) { return y - t; } private static long C1(int t) { return t - 1; } private static long Cz(int t) { return z - t; } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } void printInts(int... a) { StringBuilder sb = new StringBuilder(32); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } void printLongs(long... a) { StringBuilder sb = new StringBuilder(64); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } void printStrings(String... a) { StringBuilder sb = new StringBuilder(32); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } } }