(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries

let k, n = Scanf.sscanf (read_line ()) "%d %d" (
        fun k n -> 
                k, n
)

let lst = Str.split (Str.regexp " ") (read_line ())
          |> List.map int_of_string 
          |> List.sort compare

let rec f head tail lst =
        match lst with
                  [] -> []
                | first :: [] -> 
                        [head + (k - tail)] @ f head tail []  
                | first :: rest -> 
                        [(List.hd rest) - first] @ f head tail rest

let () =
        let tmplst = f (List.min lst) (List.max lst) lst in
        let mintmplst = List.min tmplst in
        let maxtmplst = List.max tmplst in
        let tmplst2 = 
                if  mintmplst = maxtmplst then
                        match tmplst with
                                  [] -> [0]
                                | first :: rest -> rest
                else
                        List.filter (fun elmnt -> elmnt != maxtmplst) tmplst 
        in
                List.fold_right (+) tmplst2 0
                |> Printf.printf "%d\n"
