n,k=gets.split.map &:to_i
h=gets.split.map &:to_i
dp=Array.new(n,0)
1.upto(n-1) do |i|
  min=Float::INFINITY
  min2=Float::INFINITY
  if i < k
    0.upto(i-1) do |j|
      can=dp[j]+(h[i]-h[j]).abs
      if can < min
        min=can
      end
    end
    dp[i]=min
  else
    1.upto(k) do |l|
      can2=dp[i-l]+(h[i]-h[i-l]).abs
      if can2 < min2
        min2=can2     
      end
    end
    dp[i]=min2
  end
end

puts dp.last