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

dp={0}
for i=2,n do
dp[i]=math.huge
for j=1,math.min(i-1,k) do
dp[i]=math.min(dp[i-j]+math.abs(h[i]-h[i-j]),dp[i])
end
end
print(dp[n])