open Printf
open Scanf

module L =
  struct
    include List
  
    let init n f =
      let rec loop x =
        if x = n then []
        else let y = f x in y :: loop (x + 1)
      in loop 0

    let rec map2 f xs ys =
      match xs, ys with
        [], _ -> []
      | _, [] -> []
      | x :: xs, y :: ys -> f x y :: map2 f xs ys

    let sum xs =
      fold_left (+) 0 xs

    let maximum xs =
      fold_left max 0 xs
  end

let id x = x

let solve k n =
  let la = L.init n @@ fun _ -> scanf "%d " id in
  let ld = L.map2 (-) (L.tl la) la in
  k - L.maximum ((k - L.sum ld) :: ld)

let () =
  scanf "%d %d " solve |> printf "%d\n"
