n,k = gets.split.map &:to_i
h = gets.split.map &:to_i

dp = Array.new(n,nil)
dp[0] = 0

(1..n-1).each do |i|
  dp[i] = (1..([k,i].min)).map do |j|
    dp[i-j] + (h[i] - h[i-j]).abs
  end.min
end

puts dp[n-1]