import Foundation

func readInt() -> [Int] {
    return readLine()!.componentsSeparatedByString(" ").map { Int($0)! }
}

func main() {
    let line = readInt()
    let N = line[0]
    let K = line[1]
    let H = readLine()!.componentsSeparatedByString(" ").map { Int64($0)! }

    var dp = (1...N).map {_ in Int64.max }
    dp[0] = 0

    for i in 0..<N {
        for j in 1...K {
            if i + j >= N {
                continue
            }
            dp[i+j] = min(dp[i+j], dp[i]+abs(H[i+j] - H[i]))
        }
    }
    print(dp[N-1])
}

main()