Scanf.scanf "%d %d" (fun n k -> Printf.printf "%d\n" @@ if k < 1 || k > n then 0 else if k * 2 - 1 = n then n - 1 else n - 2 )