loop do
  n = gets.to_i
  break if n == 0
  arr = n.times.map { gets.to_i }

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