n,k=gets.chomp.split(" ").map(&:to_i)
   h=gets.chomp.split(" ").map(&:to_i)
   dp=[]
   dp[0]=0
   dp[1]=(h[1]-h[0]).abs
   for i in 2..n-1
     min=99999
     for j in 1..k
       break if i-j<0
        if(dp[i-j]+(h[i]-h[i-j]).abs<min)
          min=dp[i-j]+(h[i]-h[i-j]).abs
        end
   end
    dp[i]=min
  end
  puts dp[n-1]