#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a,b) (a>b)?a:b
#define min(a,b) (a>b)?b:a
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
const int maxn = 500100;
using namespace std;
LL a[maxn],b[maxn],n,sum = 0;
/*void Sort(LL L,LL mid,LL R)
{
LL s = L,e = mid + 1;
LL num = 0;
while(s<=mid&&e<=R)
{
if(a[s]<=a[e])
{
b[num++] = a[s++];
}
else
{
b[num++] = a[e++];
sum += mid - s + 1;
}
}
while(s<=mid)
{
b[num++] = a[s++];
}
while(e<=L)
{
b[num++] = a[e++];
}
for(int i = 0;i<num;i++)
a[L+i] = b[i];
}*/
void gb_sort(LL *a,LL L,LL R)
{
if(L<R)
{
LL mid = (L+R)/2;
gb_sort(a,L,mid);
gb_sort(a,mid+1,R);
// Sort(L,mid,R);
LL s = L,e = mid + 1;
LL num = 0;
while(s<=mid&&e<=R)
{
if(a[s]<=a[e])
{
b[num++] = a[s++];
}
else
{
b[num++] = a[e++];
sum += mid - s + 1;
}
}
while(s<=mid)
{
b[num++] = a[s++];
}
while(e<=L)
{
b[num++] = a[e++];
}
for(int i = 0;i<num;i++)
a[L+i] = b[i];
}
}
int main()
{
while(scanf("%I64d",&n),n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
scanf("%I64d",&a[i]);
}
gb_sort(a,0,n-1);
cout<<sum<<endl;
}
return 0;
}