import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); long[][] costs = new long[8][8]; for (int i = 0; i < 8; i++) { Arrays.fill(costs[i], Long.MAX_VALUE / 10); costs[i][i] = 0; } costs[0][1] = x - 1; costs[0][2] = 1; costs[0][4] = 1; costs[1][3] = 1; costs[3][5] = 1; costs[2][3] = y - 1; costs[4][5] = z - 1; char sType = sc.next().charAt(0); int sIdx = sc.nextInt(); if (sType == 'A') { costs[6][0] = sIdx - 1; costs[6][1] = x - sIdx; } else if (sType == 'B') { costs[6][2] = sIdx - 1; costs[6][3] = y - sIdx; } else { costs[6][4] = sIdx - 1; costs[6][5] = z - sIdx; } char tType = sc.next().charAt(0); int tIdx = sc.nextInt(); if (tType == 'A') { costs[7][0] = tIdx - 1; costs[7][1] = x - tIdx; } else if (tType == 'B') { costs[7][2] = tIdx - 1; costs[7][3] = y - tIdx; } else { costs[7][4] = tIdx - 1; costs[7][5] = z - tIdx; } if (sType == tType) { costs[6][7] = Math.abs(sIdx - tIdx); } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { for (int k = 0; k < 8; k++) { costs[j][k] = Math.min(costs[j][k], costs[j][i] + costs[i][k]); costs[j][k] = Math.min(costs[j][k], costs[k][j]); costs[k][j] = costs[j][k]; } } } System.out.println(costs[6][7]); } }