N=int(input())
A=list(map(int,input().split()))
ans=sum(A)
s=N+1
j=s
sum_A=0

for i in range(N):
    if A==sorted(A):
        print(i)
        exit()
    
    x=max(A[:s])
    s=A.index(x)
    A[s:j]=[x]*len(A[s:j])
    j=s
    if s==0:
        break

print(sum(A)-ans)