/*
 * ultraqs.cpp
 *
 *  Created on: Sep 27, 2008
 *      Author: Chris
 */


#include <cstdio>
#include <iostream>

using namespace std;

long long values[2][500000];
int num_values;
long long num_swaps;

void merge(int begin, int mid, int end, int ind, int next_ind) {
	int p1 = begin, p2 = mid, p3 = begin;
	while (true) {
		if (values[ind][p2] < values[ind][p1]) {
			values[next_ind][p3++] =  values[ind][p2++];
			num_swaps += mid - p1;
		}
		else values[next_ind][p3++] = values[ind][p1++];
		if (p1 == mid) { p1 = p2; break; }
		if (p2 == end) { break; }
	}
	while (p3 < end) values[next_ind][p3++] = values[ind][p1++];
}

void sort(int begin, int end, int ind, int next_ind) {
	if (end - begin < 2) return;
	int mid = (begin + end) / 2;
	sort(begin, mid, next_ind, ind);
	sort(mid, end, next_ind, ind);
	merge(begin, mid, end, ind, next_ind);
}

int main() {
	while (true) {
		scanf("%d", &num_values);
		if (num_values == 0) break;
		for (int i = 0; i < num_values; i++) {
			long long in;
			scanf("%Ld", &in);
			values[0][i] = values[1][i] = in;
		}

		num_swaps = 0;
		sort(0, num_values, 0, 1);
		cout << num_swaps << "\n";
	}
	cout.flush();

	return 0;
}

