#include <cstdio>
#include <climits>
#include <algorithm>
using namespace std;

void sort_count(int n, int *a, int *b, long long &count) {
  if(n <= 1) return;
  int m0 = n/2;
  int m1 = n-m0;
  sort_count(m0, a, b, count);
  sort_count(m1, a+m0, b+m0, count);
  int i = 0, j = 0;
  while(i < m0 || j < m1) {
    int ai = i < m0 ? a[i] : INT_MAX;
    int aj = j < m1 ? a[m0+j] : INT_MAX;
    if(ai <= aj) {
      b[i++ + j] = ai;
    } else {
      b[i + j++] = aj;
      count += m0 - i;
    }
  }
  copy(b, b+n, a);
}

int main() {
  for(int tci = 0; ; tci++) {
    int N; scanf("%d", &N);
    if(!N) break;
    int *a = new int[N];
    int *b = new int[N];
    for(int i = 0; i < N; i++) scanf("%d", a+i);
    long long count = 0;
    sort_count(N, a, b, count);
    printf("%lld\n", count);
    delete[] b;
    delete[] a;
  }
  return 0;
}