N = int(input()) P = list(map(int, input().split())) total = 0 used = set() for i in range(N-1, -1, -1): p = P[i] if p != N and p+1 not in used: total += i+1 used.add(p+1) elif p not in used: used.add(p) else: total -= i+1 for j in range(1, N): if j not in used: used.add(j) break print(total)