fun main(args : Array<String>) {
    val (n, k) = readLine()!!.split(" ").map { it.toInt() }
    val hh = readLine()!!.split(" ").map { it.toInt() }

    val dp = IntArray(n){ Int.MAX_VALUE }

    for (i in 0 until n) {

        //println("h:$i")

        if (i == 0) {
            dp[i] = 0
            continue
        }

        val start = Math.max(0, i-k)
        var min = Int.MAX_VALUE

        for (j in start until i) {
            min = Math.min(min, dp[j] + Math.abs(hh[j]-hh[i]))
            //println("$j to $i /min: $min")
        }

        dp[i] = min
    }

    println(dp[n-1])
}