let judge k (a: int []) =
    let slice s e a = Array.sub a s (e - s + 1)

    Array.append a [| k + a.[0] |]
    |> Array.pairwise // 小区間の端点のペアを作る
    |> Array.map (fun (s, e) -> e - s |> abs)
    |> Array.sort
    |> slice 0 (a.Length - 2)
    |> Array.sum

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

[<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
