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

ans = 0
for i,ele in enumerate(list):
    if i == 0:
        continue
    elif i == len(list)-1:
        continue

    if ele > list[i+1]:
        ans = ans +  ele - list[i+1] + 1

print(ans)
