import java.lang.Math.*
import java.util.*

fun main(args: Array<String>) {
    val scn = Scanner(System.`in`)
    val N = scn.nextInt()
    val K = scn.nextInt()

    var h = arrayOfNulls<Int>(N + 1)
    for (i in 1..N) {
        h[i] = scn.nextInt()
    }
    h[0] = 0

    var dp = arrayOfNulls<Int>(N + 1)
    for (i in 1..N) {
        dp[i] = Int.MAX_VALUE
    }
    dp[0] = 0
    dp[1] = 0

    for (i in 2..N) {
        for (k in 1..K) {
            if (k < i) dp[i] = min(dp[i]!!, abs(h[i]!! - h[i - k]!!) + dp[i - k]!!)
        }
    }

    println(dp[N])
}