fun main(args: Array<String>) {
    edpb()
}

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

    val totalCostsToReach = Array(heights.size) { Long.MAX_VALUE }
    totalCostsToReach[0] = 0

    for (i in 1 until heights.size) {
        val minTotalCostToReach = (1..k).filter { jump -> i - jump >= 0 }.map { jump ->
            val jumpCost = Math.abs(heights[i] - heights[i - jump])
            jumpCost + totalCostsToReach[i - jump]
        }.min()!!

        totalCostsToReach[i] = minTotalCostToReach
    }

    println(totalCostsToReach[n - 1])
}
