n,k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)
k = [k,n-1].min
dp = Array.new(n){Float::INFINITY}
dp[0] = 0
(1..k).each do |i|
  dp[i] = (h[i] - h[0]).abs
end
(k+1...n).each do |i|
  (1..k).each {|j| dp[i] = [dp[i-j] + (h[i] - h[i-j]).abs,dp[i]].min}
end
p dp[n-1]
