// Arup Guha
// 12/31/2017
// Solution to 2017 Dec USACO Contest Problem: Greedy Gift Takers

import java.util.*;
import java.io.*;

public class greedy {

	public static int n;
	public static int[] vals;

	public static void main(String[] args) throws Exception {

		// Read the data.
		BufferedReader stdin = new BufferedReader(new FileReader("greedy.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		n = Integer.parseInt(tok.nextToken());
		vals = new int[n];
		tok = new StringTokenizer(stdin.readLine());
		for (int i=0; i<n; i++)
			vals[i] = Integer.parseInt(tok.nextToken());

		// Run binary search.
		int low = 1, high = n;
		while (low < high) {
			int mid = (low+high)/2;
			if (test(mid))
				high = mid;
			else
				low = mid+1;
		}

		// Ta da!
		PrintWriter out = new PrintWriter(new FileWriter("greedy.out"));
		out.print(n-low);
		out.close();
		stdin.close();
	}

	// Returns true iff person limit never gets to go.
	public static boolean test(int limit) {

		// Fill in our bit with each item upto limit.
		bit mybit = new bit(n);
		for (int i=0; i<limit; i++)
			mybit.add(vals[i]+1,1);

		// See if there is a group of k+1 people who will always
		// occupy the first k+1 slots.
		for (int k=0; k<limit; k++)
			if (mybit.atOrAbove(n-k) >= k+1)
				return true;

		// If we get here, this isn't the case.
		return false;
	}
}

class bit {

	public long[] cumfreq;

	// Do indexes 1 to n.
	public bit(int n) {

		int size = 1;
		while (size < n) size <<= 1;
		n = size;

		cumfreq = new long[n+1];
	}

	// Uses 1 based indexing.
	public void add(int index, long value) {
		while (index < cumfreq.length) {
			cumfreq[index] += value;
			index += Integer.lowestOneBit(index);
		}
	}

	// Returns the sum of everything upto index.
	public long sum(int index) {
		long ans = 0;
		while (index > 0) {
			ans += cumfreq[index];
			index -= (Integer.lowestOneBit(index));
		}
		return ans;
	}

	// Use 1 based indexing.
	public long sum(int low, int high) {
		return sum(high) - sum(low-1);
	}

	// Return the total number of items in the BIT.
	public long all() {
		return sum(cumfreq.length-1);
	}

	// Return the total number of items in the BIT at or above index.
	public long atOrAbove(int index) {
		long sub = 0;
		if (index > 0) sub = sum(index-1);
		return all() - sub;
	}
}