#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define SIZE 500010
int countNum[SIZE];
struct Node{
Node(): val( 0 ), ID( 0 ){};
int val;
int ID;
};
int lowBit( const int& index ){
return index & ( - index );
}
void update( int index, const int& val ){
while( index < SIZE - 1 ){
countNum[index] += val;
index += lowBit( index );
}
}
int getSum( int index ){
int sum = 0;
while( index >= 1 ){
sum += countNum[index];
index -= lowBit( index );
}
return sum;
}
int cmp( const Node& a, const Node& b ){
return a.val > b.val;
}
int main(){
while( true ){
memset( countNum, 0, sizeof( countNum ) );
int num;
int i = 0;
long long sum = 0;
cin>>num;
if( num == 0 )
break;
Node* vec = new Node[num + 2];
for( i = 0; i < num; ++i ){
vec[i].ID = i + 1;
cin >> vec[i].val;
}
sort( vec, vec+num, cmp );
for( i = 0; i < num; ++i ){
sum += getSum( vec[i].ID );
update( vec[i].ID, 1 );
}
cout << sum << endl;
delete vec;
}
return 0;
}