import kotlin.math.*

fun main() {
  val (N, K) = readLine()!!.split(" ").map(Integer::parseInt)
  val h = readLine()!!.split(" ").map(Integer::parseInt)

  var dp = IntArray(N) { 0 }
  dp[1] = abs(h[0] - h[1])

  var n = 2
  while (true) {
    if (n == N) break
    val maxK = min(K, n)
    var cost = Integer.MAX_VALUE
    for (k in 1..maxK) {
      cost = min(cost, abs(h[n-k] - h[n]) + dp[n-k])
    }
    dp[n] = cost
    n++
  }

  println(dp[N-1])
}