import scala.io.StdIn.readInt
import scala.collection.mutable.ArrayBuffer

object Main {
  def swap(a:Array[Int],i:Int,j:Int) = {
    val temp = a(i)
    a(i) = a(j)
    a(j) = temp
  }

  def bubbleSort(a:Array[Int]) = {
    var cnt = 0
    for(i<-0 until a.length){
      for(j<-0 until a.length-i-1) {
        if(a(j) > a(j+1)) {
          swap(a,j,j+1)
          cnt+=1
        }
      }
    }
    cnt
  }

  def main(args:Array[String]) = {
    var n = 0
    val b = new ArrayBuffer[Int]
    while({n=readInt;n}!=0) {
      val a = for(i<-1 to n) yield readInt
      b += bubbleSort(a.toArray)
    }
    println(b.mkString("\n"))
  }
}