Scanf.scanf "%d %d %d %d" (fun n m p q -> let rec loop i rest = let cur = (i mod 12) + 1 in let rest = if cur >= p && cur < p + q then rest - 2 * m else rest - m in if rest <= 0 then i + 1 else loop (i + 1) rest in loop 0 n |> Printf.printf "%d\n" )