[<AutoOpen>]
module Program

open System

let read f = stdin.ReadLine() |> f
let reads f = stdin.ReadLine().Split() |> Array.toList |> List.map f

[<EntryPoint>]
let main _ =
    let [n;k] = reads int
    let hs = reads int |> List.toArray

    let dp = Array.create n 0
    for i = 1 to n - 1 do
        seq {
            for j = 1 to k do
            if i - j >= 0 then
                yield dp.[i-j] + abs (hs.[i] - hs.[i-j])
        }
        |> Seq.min
        |> fun x -> dp.[i] <- x

    printfn "%i" dp.[n-1]
    0