n, k = gets.chomp.split.map(&:to_i)
h = gets.chomp.split.map(&:to_i)
INF = 10 ** 9

dp = Array.new(10 ** 5 + 5, INF)
dp[0] = 0

1.upto(n-1) do |i|
  min = INF
  (i-1).downto([i-k,0].max) do |j|
    min = [min, dp[j] + (h[i] - h[j]).abs].min
  end
  dp[i] = min
end
p dp[n-1]
