INF = 10**8
N, K = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)

dp = Array.new(N, INF)
dp[0] = 0
N.times do |i|
  1.upto([K, (N - 1) - i].min) do |k|
    # if i + k <= N - 1
    #   dp[i + k] = [dp[i + k], dp[i] + (arr[i] - arr[i + k]).abs].min
    # end
    dp[i + k] = dp[i] + (arr[i] - arr[i + k]).abs if dp[i + k] > dp[i] + (arr[i] - arr[i + k]).abs
  end
end
puts dp[N-1]