import io.StdIn._
import math._

object Main extends App {
  val Array(n, k) = readLine() split(' ') map(_.toInt)
  val hs = readLine() split(' ') map(_.toInt)

  val dp = new Array[Long](n)
  dp(0) = 0

  for (i <- 1 until min(n,k)) {
    dp(i) = (1 to i).map(j => dp(i-j)+abs(hs(i)-hs(i-j))).min
  }

  if (k <= n)
  for (i <- k until n) {
    dp(i) = (1 to k).map(j => dp(i-j)+abs(hs(i)-hs(i-j))).min
  }

  println(dp.last)
}