str1=io.read()
str2=io.read()
info={}
h={}
for w in string.gmatch(str1,"[^ ]+")do
	table.insert(info,w)
end
for w in string.gmatch(str2,"[^ ]+")do
	table.insert(h,w)
end
memo={}
memo[1]=0
function cal(i,n,k)
	if(memo[i]~=nil)then
		return memo[i]
	end
	local list={}
	local min=999999999
	for j=i-1,i-k,-1 do
		if(j<1)then
			do
				break
			end
		end
		list[j]=cal(j,n,k)+math.abs(h[i]-h[j])
		if(list[j]<min)then
			min=list[j]
		end
	end
	if(min~=999999999)then
		memo[i]=min
	end
	return min
end
local a={}
print(math.floor(cal(tonumber(info[1]),tonumber(info[1]),tonumber(info[2]))))