let judge k a =
    min (Array.reduceBack (-) a)
        (Array.append [| a.[a.Length - 1] |] (a.[0..(a.Length - 2)] |> Array.map ((+) k))
         |> Array.reduceBack (-))

//let input = [| 20, 3, [| 5; 10; 15 |]; 20, 3, [| 0; 5; 15 |] |]
//for (k, _, a) in input do judge k a |> printfn "%d"

[<EntryPoint>]
let main argv =
    let k = stdin.ReadLine().Split() |> fun x -> x.[0] |> int
    let a = stdin.ReadLine().Split() |> Array.map int
    judge k a |> printfn "%d"
    0
