private fun readInts() = readLine()!!.split(" ").map(String::toInt)

fun main(args: Array<out String>) {
    val (N, K) = readInts()
    val h = readInts()

    val costs = Array(N) { 0 }

    (0 until N).forEach { index ->
        costs[index] = (1..K)
            .filter { k -> index - k >= 0 }
            .map { k -> costs[index - k] + Math.abs(h[index] - h[index - k]) }
            .min()
            ?: 0
    }

    println(costs[N - 1])
}