open System

let R() = Console.ReadLine().Split() |> Array.map int

let f (k: int) (n: int) (a: int list): int =
    let a' = let h = List.last a - k in h :: a 
    List.pairwise a' 
    |> List.map(fun (l,r) -> k - r + l)
    |> List.min 

let K, N = let t = R() in t.[0], t.[1]
let A = R()

f K N (List.ofArray A)
|> printfn "%i"
