import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int[] counts = new int[3]; for (int i = 0; i < 3; i++) { counts[i] = sc.nextInt(); } int[] target = new int[2]; int[] station = new int[2]; for (int i = 0; i < 2; i++) { target[i] = sc.next().charAt(0) - 'A'; station[i] = sc.nextInt(); } long[][] distance = new long[8][8]; for (int i = 0; i < 8; i++) { Arrays.fill(distance[i], Long.MAX_VALUE / 2); distance[i][i] = 0; } for (int i = 0; i < 3; i++) { distance[i * 2][i * 2 + 1] = counts[i] - 1; distance[i * 2 + 1][i * 2] = counts[i] - 1; distance[i * 2][(i + 1) % 3 * 2] = 1; distance[i * 2 + 1][(i + 1) % 3 * 2 + 1] = 1; } for (int i = 0; i < 2; i++) { distance[target[i] * 2][6 + i] = station[i] - 1; distance[6 + i][target[i] * 2] = station[i] - 1; distance[target[i] * 2 + 1][6 + i] = counts[target[i]] - station[i]; distance[6 + i][target[i] * 2 + 1] = counts[target[i]] - station[i]; } if (target[0] == target[1]) { distance[6][7] = Math.abs(station[0] - station[1]); distance[7][6] = Math.abs(station[0] - station[1]); } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { for (int k = 0; k < 8; k++) { distance[j][k] = Math.min(distance[j][k], distance[j][i] + distance[i][k]); } } } System.out.println(distance[6][7]); } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }