input = []
while (n = gets.to_i) != 0 do
  input = gets.split.map(&:to_i)
end

count = 0
for flag in 0..input.size-1 do
  for i in -input.size + 1..-flag do
    j = -i
    if input[j] < input[j-1] && j != 0
      tmp = input[j]
      input[j] = input[j-1]
      input[j-1] =tmp
      count += 1
    end
  end
end
puts input*" "
puts count
