n = int(input()) p = list(map(int, input().split())) p = list(reversed(p)) takeda = [x for x in range(1, n+1)] ansT, ansS = 0, 0 for i in range(n): score = n-i tmp = p[i] while True: tmp += 1 if tmp in takeda: ansT += score takeda.remove(tmp) break if tmp > max(takeda): if p[i] in takeda: takeda = takeda[1:] else: ansS += score takeda = takeda[1:] break print(ansT - ansS)