import heapq N = int(input()) A = list(map(int,input().split())) A = A[::-1] ans = 0 count = 1 free = [] while(A): while(free and free[0] == count): heapq.heappop(free) count += 1 if(A[-1] == count): A.pop() count += 1 else: ans += 1 heapq.heappush(free,A.pop()) print(ans)