#encoding:utf-8 No.0167 2014.10.15
def bubble_sort_count ary
  sorted = []
  count = 0
  until ary.length == 0
    i = 0
    while i < ary.length - 1
      if ary[i] > ary[i+1]
        ary[i],ary[i+1] = ary[i+1],ary[i]
        count += 1
        i += 1
      else
        i += 1
      end
    end
    sorted.unshift ary.pop
  end
  return count
end

until (n = gets.chomp.to_i) == 0
  numbers = Array.new(n)
  n.times do |i|
    numbers[i] = gets.chomp.to_i
  end
  puts bubble_sort_count numbers
end