N = map(int, input().split())
l = list(map(int, input().split()))
from collections import deque


l = deque(l)

count=0
for i in range(len(l)-1):
    while l[i] >l[i+1]:
        l[i+1]+=1
        count+=1
print(count)