n, k = gets.chomp.split.map(&:to_i)
hs = gets.chomp.split.map(&:to_i)
dp = Array.new(n, Float::INFINITY)

dp[0] = 0
1.upto(n-1) do |i|
  1.upto(k) do |j|
    next if i -j < 0
    cost = dp[i-j] + (hs[i] - hs[i-j]).abs
    dp[i] = [dp[i], cost].min
  end
end

puts dp[n-1]