N,K=gets.split.map(&:to_i)
h=gets.split.map(&:to_i)
dp=Array.new(N,10**4)
dp[0]=0
for i in 1..N-1
    for j in 1..K
        if i<j
            break
        end
        a=dp[i-j]+(h[i-j]-h[i]).abs
        dp[i]=(dp[i]>a ? a : dp[i])
    end
end
puts dp[N-1]