import java.lang.Math.*

fun main(args: Array<String>) {
    val (N, K) = readLine()!!.split(" ").map {it.toInt()}
    val a = readLine()!!.split(" ").map{it.toInt()}
    val dp = Array(N){1000000000}
    dp[0] = 0
    for(i in 1 until N) {
        for(j in i - 1 downTo max(0, i - K)) {
            dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]))
        }
    }
    println(dp[N-1])
}
