local n,k=io.read("n","n")
local h={}
for i=1,n do
    h[i]=io.read("n")
end

local INF=10^20
local dp={0}
for i=2,n do
    dp[i]=INF
end

for i=1,n do
    for j=1,k do
        if i+j<=n then
            dp[i+j]=math.min(dp[i+j],dp[i]+math.abs(h[i]-h[i+j]))
        end
    end
end
print(dp[n])