open Batteries
let rec sum lst =
  match lst with
  | [] -> 0
  | first :: rest -> first + sum rest
let kn = Scanf.sscanf (read_line()) "%d %d" (fun k n ->
  [|k; n|]
)
let k = kn.(0)
let n = kn.(1)
let a = read_line () |> String.split_on_char ' ' |> List.map int_of_string |> Array.of_list
let rec get_lengths i =
  if i = (Array.length a) - 1 then [(k-a.(i))+a.(0)] else
  a.(i+1) - a.(i) :: get_lengths (i+1)

let lengths = List.sort compare @@ get_lengths 0
let _ = Printf.printf "%d\n" (sum @@ List.tl @@ List.rev lengths)
