Scanf.scanf "%d %d" (fun k n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let d = Array.init n (fun i ->
        let r = a.((i + 1) mod n) - a.(i) in
        if r < 0 then r + k else r
        ) in
    Array.sort compare d;
    Printf.printf "%d\n" (k - d.(n - 1))
)