def bubble_sort(arr)
  arr.size.times do |i|
    0.upto(arr.size-i-2) do |j|
      if arr[j] > arr[j+1]
        arr[j], arr[j+1] = arr[j+1], arr[j]
        @c += 1
      end
    end
  end
end

while gets do
  n = $_.to_i
  break if n == 0
  arr = n.times.map{ gets }
  @c = 0
  bubble_sort(arr)
  puts @c
end