n = int(input())
a = list(map(int, input().split()))
 
x = 0
for i in range(n-1):
  while True:
    if a[i] > a[i + 1]:
      a[i + 1] += 1
      x += 1
    if a[i] <= a[i + 1]:
      break
 
print(x)