N, K = gets.split.map(&:to_i)
H = gets.split.map(&:to_i)
dp = Array.new(N, 0)
1.step(N-1) do |i|
  min = Float::INFINITY
  1.step(K) do |s|
    break unless i >= s
    p = (H[i-s] - H[i]).abs + dp[i-s]
    min = p if min > p
  end
  dp[i] = min
end
puts dp.last