n,k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)

dp = []
dp[0] = 0
dp[1] = (h[0] - h[1]).abs

1.upto(n-1) { |i|
  m = 10**12
  k.times { |_k|
    ind = i - 1 - _k
    if (ind >= 0)
      tmp = dp[ind] + (h[ind] - h[i]).abs
      if tmp < m
        m = tmp
      end
    end
  }
  dp[i] = m
}


puts dp[n-1]
