open Scanf open Printf let pair x y = x, y let triple x y z = x, y, z let mins = List.fold_left min max_int let rec solve x y z s0 t0 s1 t1 = let fhead s k = match s with | "A" -> min (2*(k-1) + 1) (2*(x-k) + 3 + min (2*y-2) (2*z-2)) | "B" -> min (2*(k-1) + 1) (2*(y-k) + 3 + min (2*x-2) (2*z-2)) | _ -> min (2*(k-1) + 1) (2*(z-k) + 3 + min (2*x-2) (2*y-2)) in let ftail s k = match s with | "A" -> min (2*(k-1) + 3 + min (2*y-2) (2*z-1)) (2*(x-k) + 1) | "B" -> min (2*(k-1) + 3 + min (2*x-2) (2*z-1)) (2*(y-k) + 1) | _ -> min (2*(k-1) + 3 + min (2*x-2) (2*y-1)) (2*(z-k) + 1) in if s0 = s1 then mins [2 * abs (t1 - t0); fhead s0 t0 + fhead s1 t1; ftail s0 t0 + ftail s1 t1] else mins [fhead s0 t0 + fhead s1 t1; ftail s0 t0 + ftail s1 t1] let () = let x, y, z = scanf " %d %d %d" triple in let s0, t0 = scanf " %s %d" pair in let s1, t1 = scanf " %s %d" pair in printf "%d\n" @@ (solve x y z s0 t0 s1 t1) / 2