/*Bubble Sort*/
/*This program uses the tree structure(Binary Indexed Tree).
We can calculate nuber of exchanges for O(log n)
total order of this program is O(n log n) because of sort(quick sort)*/
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long Int;
#define SIZE 100001
int bit[SIZE];

int n;

int sum(int i) {
	int res = 0;
	for (int x = i;x > 0;x -= x&-x) {
		res += bit[x];
	}
	return res;
}

void add(int i, int r) {
	for (int x = i;x <= n;x += x&-x)bit[x] += r;
}

int main() {
	int a[SIZE];
	int b[SIZE];
	int lb, ub, mid;
	Int res;

	while (true) {
		scanf("%d", &n);
		if (n == 0)break;
		for (int i = 0;i < n;i++) {
			scanf("%d", &a[i]);
		}

		for (int i = 0;i < n;i++) {
			b[i] = a[i];
		}
		sort(b, b + n);
		for (int i = 0;i < n;i++) {//change a[i] to k(a[i] is k.th largest of the array)
			lb = -1;ub = n;
			while (ub - lb > 1) {
				mid = (ub + lb) / 2;
				if (a[i] > b[mid]) {
					lb = mid;
				}
				else {
					ub = mid;
				}
			}
			a[i] = ub + 1;
		}
		for (int i = 0;i <= n;i++) {
			bit[i] = 0;
		}
		res = 0;
		for (int i = 0;i < n;i++) {
			res += i - sum(a[i]);
			add(a[i], 1);
		}
		printf("%lld\n", res);
	}

	return 0;
}