n = int(input())
height = []
height = input().split()
goukei = 0
count = 0
takasa = []
for i in range(len(height)):
    takasa.append(int(height[i]))
    count = 0
    while(i!=0 and takasa[i]<=takasa[i-1]-1):
        takasa[i] += 1
        count += 1
    goukei = goukei + count

print(goukei)
