n = int(input())
a = list(input().split(" "))
a = [int(_) for _ in a]
cnt = 0
fa = [0 for i in range(len(a))]
while True:
    for k in range(1,len(a)):
        if a[k-1] > a[k]:
            a[k] = a[k] + 1
            cnt = cnt + 1
        else:
            fa[k-1] = 1
    if all(fa[0:-1]) == 1 and fa[-1] == 0:
        break
print(cnt)