#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int mergecount(vector<int> &a){
  int count = 0;
  int n = a.size();
  if(n > 1){
    vector<int> b(a.begin(),a.begin() + n/2);
    vector<int> c(a.begin() + n/2, a.end());
    count += mergecount(b);
    count += mergecount(c);
    for(int i=0,j=0,k=0;i<n;i++){
      if(k == c.size()) a[i] = b[j++];
      else if(j == b.size()) a[i] = c[k++];
      else if(b[j] <= c[k]) a[i] = b[j++];
      else {a[i] = c[k++]; count += n/2 - j;}
    }
  }
  return count;
}

int main(){
  int n;
  while(cin>>n,n){
    vector<int> v(n);
    rep(i,n)cin>>v[i];
    cout<<mergecount(v)<<endl;
  }
}