loop do
  n = gets.to_i
  break if n == 0

  arr = []
  n.times { arr << gets.to_i }

  count = 0
  0.upto(n-1) do |i|
    0.upto(n-2-i) do |j|
      if arr[j] > arr[j+1]
        arr[j], arr[j+1] = arr[j+1], arr[j]
        count += 1
      end
    end
  end

  puts count
end