open System

let readInts () = stdin.ReadLine().Split(' ') |> Array.map int64
let uncurry f (a,b) = f a b

let args = readInts()
let k, n = args.[0], int args.[1]
let a = readInts()
let b = Array.append (Array.tail a) [|a.[0]+k|]
let m = Array.map2 (-) b a |> Array.max
printfn "%d" (k - m)