n, k = gets.rstrip.split.map(&:to_i)
hs = gets.rstrip.split.map(&:to_i)

def chmin(i, b); $dp[i]=b if $dp[i] > b; end

$dp = Array.new(n, Float::INFINITY)
$dp[0] = 0

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