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(); char stype = sc.next().charAt(0); int s = sc.nextInt(); char ttype = sc.next().charAt(0); int t = sc.nextInt(); long[][] dist = new long[8][8]; for (int i = 0; i < 8; i++) { Arrays.fill(dist[i], Long.MAX_VALUE / 10); dist[i][i] = 0; } dist[0][2] = 1; dist[0][4] = 1; dist[2][4] = 1; dist[1][3] = 1; dist[1][5] = 1; dist[3][5] = 1; dist[0][1] = x - 1; dist[2][3] = y - 1; dist[4][5] = z - 1; dist[2][0] = 1; dist[4][0] = 1; dist[4][2] = 1; dist[3][1] = 1; dist[5][1] = 1; dist[5][3] = 1; dist[1][0] = x - 1; dist[3][2] = y - 1; dist[5][4] = z - 1; if (stype == 'A') { dist[0][6] = s - 1; dist[6][0] = s - 1; dist[1][6] = x - s; dist[6][1] = x - s; } else if (stype == 'B') { dist[2][6] = s - 1; dist[6][2] = s - 1; dist[3][6] = y - s; dist[6][3] = y - s; } else { dist[4][6] = s - 1; dist[6][4] = s - 1; dist[5][6] = z - s; dist[6][5] = z - s; } if (ttype == 'A') { dist[0][7] = t - 1; dist[7][0] = t - 1; dist[1][7] = x - t; dist[7][1] = x - t; } else if (ttype == 'B') { dist[2][7] = t - 1; dist[7][2] = t - 1; dist[3][7] = y - t; dist[7][3] = y - t; } else { dist[4][7] = t - 1; dist[7][4] = t - 1; dist[5][7] = z - t; dist[7][5] = z - t; } if (stype == ttype) { dist[6][7] = Math.abs(s - t); dist[7][6] = Math.abs(s - t); } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { for (int k = 0; k < 8; k++) { dist[j][k] = Math.min(dist[j][k], dist[j][i] + dist[i][k]); } } } System.out.println(dist[6][7]); } }