n, k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)
dp = [0]
n.times do |idx|
  (1..k).each do |aidx|
    break if h[idx+aidx].nil?
    dp[idx+aidx] = dp[idx+aidx].nil? ? dp[idx] + (h[idx+aidx] - h[idx]).abs : [dp[idx+aidx], dp[idx] + (h[idx+aidx] - h[idx]).abs].min
  end
end
puts dp[n-1]
