while True:
    N = input()
    if N == 0: break
    L = [input() for i in range(N)]
    c = 0
    for i in range(len(L[:])-1):
        for i in range(len(L)-1):
            if L[i] > L[i+1]:
                L[i], L[i+1] = L[i+1], L[i]
                c += 1
        else:
            del L[-1]
    else:
        print c