loop do
  n = gets.to_i
  break if n.zero?
  c, xs = 0, n.times.map { gets.to_i }
  n.downto 2 do |m|
    (m-1).times do |i|
      if xs[i] > xs[i+1]
        c += 1
        xs[i], xs[i+1] = xs[i+1], xs[i]
      end
    end
  end
  p c
end