n,k = gets.split.map &:to_i
h = gets.split.map &:to_i
 
INF = 10**12
dp = Array.new(n,INF)
dp[0] = 0
 
1.upto(n-1) do |i|
  min = 10000000001
  t = i - k
  t = 0 if t < 0
  t.upto(i-1) do |j|
    cost = (h[i] - h[j]).abs + dp[j]
    min = cost if min > cost
  end
  dp[i] = min
end
 
puts dp[-1]