#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#include <climits>
#include <functional>

#define REP(i,n) for(int i = 0;i < n;i++)

using namespace std;
typedef long long ll;
const int INF = INT_MAX / 4;

const int max_a = 1e6;


class BIT
{
public:
  vector<int> bit;
  int size;

  BIT(int size)
    : bit(vector<int>(size + 1, 0)), size(size)
  {}

  int sum(int i) {
    int r = 0;
    while (i > 0) {
      r += bit[i];
      i -= (i & -i);
    }
    return r;
  }

  void add(int i, int x) {
    while (i <= size) {
      bit[i] += x;
      i += (i & -i);
    }
  }
};


int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;

    int a[N];
    REP(i, N) cin >> a[i];
    BIT bit(max_a);
    int ans = 0;
    REP(i, N) {
      ans += i - bit.sum(a[i]);
      bit.add(a[i], 1);
    }
    cout << ans << "\n";
  }
}