#include <stdio.h>
#include <cstring>
const int sz=200005;
int ANS;
struct seg_tree{
	int l,r;
	int tag;//trueÐèÒª¸üÐÂ£¬flaseÎÞÐè¸üÐÂ 
//	int val;//µ±Ç°ÊýÇ°Ãæ±ÈËü´óµÄÊýµÄ¸öÊý 
	int exist;//Çø¼äÄÚÒÑ¾­´æÔÚÊýµÄ¸öÊý 
	int len() {return r-l+1;}
	int mid() {return r+l>>1;}
} tree[sz*4];
int n;
void build_tree(int p,int l,int r){
	tree[p].l=l,tree[p].r=r;
	if(l==r) return;
	int mid=tree[p].mid();
	build_tree(p<<1,l,mid);
	build_tree(p<<1|1,mid+1,r);
}

int ask_exist(int p,int l,int r){
	if(l<=tree[p].l && r>=tree[p].r) return tree[p].exist;
	int ans=0;
	int mid=tree[p].mid();
	if(l<=mid) ans+=ask_exist(p<<1,l,r);
	if(r>mid) ans+=ask_exist(p<<1|1,l,r);
	return ans;
}
void change(int p,int l,int r){
	if(l<=tree[p].l && r>=tree[p].r){
		if(ask_exist(1,l,n)>0 && l>ANS) ANS=l;//¸üÐÂ´ð°¸ 
		tree[p].exist=1;
		return;
	}
	int mid=tree[p].mid();
	if(l<=mid) change(p<<1,l,r);
	if(r>mid) change(p<<1|1,l,r);
	tree[p].exist=tree[p<<1].exist+tree[p<<1|1].exist;
}
int main(){
	scanf("%d",&n);
	build_tree(1,1,n);
	int x;
	for(int i=0;i<n;i++){
		scanf("%d",&x);
		change(1,x,x);
	}
//	printf("#####\n");
	printf("%d\n",ANS);
	return 0;
}
