n,k = gets.split.map(&:to_i)
hs = gets.split.map(&:to_i)
hs.unshift(0)
dp=[]
dp[1]=0
for i in 2..[k+1,n].min
  dp[i]=(1..i-1).map{|x|dp[x]+(hs[i]-hs[x]).abs}.min
end

for i in k+2..n
  dp[i]=(i-k..i-1).map{|x|dp[x]+(hs[i]-hs[x]).abs}.min
end

puts dp[n]
