#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int a[120];
int n;
int cnt =0;
void bubblesort(){
	for(int i = n-1;i >0;i--){
		for(int j =0;j < i;j++){
			if(a[j+1]<a[j]){
				swap(a[j],a[j+1]);
				cnt++;
			}
		}
	}
}

int main(){

	while(cin>>n,n){
		cnt = 0;
		for(int i =0;i <120;i++){
			a[i]=0;
		}
		for(int i =0;i < n;i++){
			cin>>a[i];
		}
		/*rep(i,n){
			cout <<a[i];
			if(i!=n-1)cout <<" ";
		}cout <<endl;*/
		bubblesort();
		/*rep(i,n){
			cout <<a[i];
			if(i!=n-1)cout <<" ";
		}
		cout <<endl;*/
		cout <<cnt<<endl;
	}
	return 0;
}