import kotlin.math.abs
import kotlin.math.min

fun main() {
    val (n, k) = readLine()!!.split(" ").map { it.toInt() }
    val h = readLine()!!.split(" ").map { it.toInt() }

    val dp = IntArray(n + 1) { Int.MAX_VALUE }
    dp[0] = 0
    dp[1] = 0
    for (i in 1..n) {
        for (j in 1..k) {
            if (i + j > n) break
            dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j - 1] - h[i - 1]))
        }
    }
    println(dp[n])
}