import scala.io.StdIn.readLine

object Main{
  def main(args : Array[String]): Unit ={
    val Array(n, k) : Array[Int] = readLine().split(' ').map(_.toInt)
    val H : Array[Int] = readLine().split(' ').map(_.toInt)
    // val H : Array[Int] = Array.fill(n)(readLine().toInt)
    if(n >= k) subMain(n, H, k).solve()
    else subMain(n, H, n).solve()
  }
}

case class subMain(n : Int, H : Array[Int], k : Int){
  val DP : Array[Int] = Array.fill(n)(Int.MaxValue)
  def CostOfIandJ(i : Int)(j : Int): Int = math.abs( H(i) - H(j) )
  def calculate(m : Int): Unit ={
    DP(0) = 0
    for(i <- 1 until k){
      def subCalculate(l : Int) = CostOfIandJ(i)(l)
      for(j <- 0 until i){
        val tmp : Int = subCalculate(j) + DP(j)
        DP(i) = math.min( DP(i), tmp )
      }
    }
    for(i <- k to m){
      def subCalculate(l : Int) = CostOfIandJ(i)(l)
      for(j <- i - k until i){
        val tmp : Int = subCalculate(j) + DP(j)
        DP(i) = math.min( DP(i), tmp )
      }
    }
  }
    /* too deep
    if(DP(m) != -1) return
    if(m == 0){
      DP(0) = 0
    }
    else{
      calculate(m - 1)
      def subCalculate(l : Int) = CostOfIandJ(m)(l)
      var MiN : Int = Int.MaxValue
      if(m < k){
        for(i <- 0 until m){
          val tmp = subCalculate(i) + DP(i)
          MiN = math.min(MiN, tmp)
        }
      }
      else{
        for(i <- m - k until m){
          val tmp = subCalculate(i) + DP(i)
          MiN = math.min( MiN , tmp )
        }
      }
      DP(m) = MiN
    }
  }*/
  def solve():Unit ={
    calculate(n - 1)
    println(DP(n - 1))
  }
}