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[] sizes = new int[3]; for (int i = 0; i < 3; i++) { sizes[i] = sc.nextInt(); } long[][] distances = new long[8][8]; for (int i = 0; i < 8; i++) { Arrays.fill(distances[i], Long.MAX_VALUE / 2); distances[i][i] = 0; } for (int i = 0; i < 3; i++) { distances[i * 2][i * 2 + 1] = sizes[i] - 1; distances[i * 2 + 1][i * 2] = sizes[i] - 1; } for (int i = 0; i < 3; i++) { distances[i * 2][(i * 2 + 2) % 6] = 1; distances[(i * 2 + 2) % 6][i * 2] = 1; distances[i * 2 + 1][(i * 2 + 3) % 6] = 1; distances[(i * 2 + 3) % 6][i * 2 + 1] = 1; } int prevIdx = -1; int prevStation = -1; for (int i = 0; i < 2; i++) { int idx = sc.next().charAt(0) - 'A'; int station = sc.nextInt(); distances[6 + i][idx * 2] = station - 1; distances[idx * 2][6 + i] = station - 1; distances[6 + i][idx * 2 + 1] = sizes[idx] - station; distances[idx * 2 + 1][6 + i] = sizes[idx] - station; if (idx == prevIdx) { distances[6][7] = Math.abs(station - prevStation); distances[7][6] = Math.abs(station - prevStation); } prevIdx = idx; prevStation = station; } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { for (int k = 0; k < 8; k++) { distances[j][k] = Math.min(distances[j][k], distances[j][i] + distances[i][k]); } } } System.out.println(distances[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(); } }