#include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n;
	while(cin>>n){
		if(n==0)break;
		int dt[n];
		rep(i,n){
			cin>>dt[i];
		}
		int cnt=0;
		while(1){
			if(n==1)break;
			rep(i,n-1){
				if(dt[i]>dt[i+1]){
					int alt=dt[i];
					dt[i]=dt[i+1];
					dt[i+1]=alt;
					cnt++;
				}
			}
			n--;
		}
		cout<<cnt<<endl;
	}
	return 0;
}