x = list(map(int, input().split()))

print(x)

A = x[1:]
step = 0
num_min = 1000

for i in range(len(A)-1):
    if A[i] > A[i+1]:
        for j in range(A[i] - A[i+1]):
            step += 1
            A[i+1] += 1
    else: continue
    
print(step)