import scala.io.StdIn._
object Main extends App {
  val Array(n, k) = readLine.split(" ").map(_.toInt)
  val h = readLine.split(" ").map(_.toInt)

  val dp = Array.fill(n)(Int.MaxValue)
  dp(0) = 0
  for (i <- 0 until n; j <- 1 to k if (i + j) < n) {
    dp(i + j) = math.min(dp(i + j), dp(i) + math.abs(h(i) - h(i + j)))
  }
  println(dp(n - 1))
}
