object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toInt)
  val h = io.StdIn.readLine().split(' ').map(_.toInt)
  val dp = Array.fill(n)(-1)
  for (i <- 0 until n) {
    if (i == 0) dp(i) = 0
    else {
      val cost = ((if (k <= i) i-k else 0) until i).map(j => dp(j) + Math.abs(h(i) - h(j))).min
      dp(i) = cost
    }
  }
  println(dp(n-1))
}