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

INF=100000000
dp=Array.new(n,INF)

dp[0]=0
for i in 1..n-1
    if i==1
        dp[i]=(h[i]-h[i-1]).abs
    else
    dp[i]=[]
    for j in 1..k
        if j>=i
        break
        end
        dp[i]<<dp[i-j]+(h[i-j]-h[i]).abs
        
    end

    dp[i]=dp[i].min
   
    end
    
end

p dp[-1]