input()
nums = [int(x) for x in input().split()]

count = 0

def check(x):
    global count
    while nums[x] > nums[x + 1]:
        nums[x + 1] += 1
        count += 1

for x in range(len(nums) - 1):
    check(x)

print(count)