n = int(input()) p = list(map(int, input().split())) ans = n * (n + 1) // 2 for i in range(n): if p[i] == n: ans -= (i + 1) * 2 print(max(0, ans))