#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i,n) _rep2((i),0,(n))
#define _rep2(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int n;
	cin>>n;
	// srand(time(NULL));
	// int m = rand()%300+200;
	int m = 6;
	vint v;
	rep(i,m){
		v.PB(rand()%1000000000);
	}
	rep(i,n){
		int x;
		cin>>x;
		v.PB(x);
	}
	// rep(i,n+m){
	// 	cout<<v[i]<<endl;
	// }
	set<pii> se;
	se.insert(pii(INF,1));
	vector<pii> w(n+m, pii(-1,-1));
	rep(i,n+m){
		if(i==m){
			// cout<<se.size()<<endl;
			// for(auto x: se){
			// 	cout<<x.fi<<" "<<-x.se<<endl;
			// }
		}
		auto it = se.lower_bound(pii(v[i],-i));
		if(*it < pii(v[i], -i)) continue;
		if(it != se.begin()){
			it--;
			w[-it->se] = pii(v[i], -i);
			it++;
		}
		if(it->fi != INF){
			se.erase(it);
		}
		se.insert(pii(v[i], -i));
	}
	// cout<<se.size()<<endl;
	// for(auto x: se){
	// 	cout<<x.fi<<" "<<-x.se<<endl;
	// }
	rep(i,m){
		if(*se.begin() == pii(v[i], -i)){
			se.erase(se.begin());
			continue;
		}
		if(se.find(pii(v[i], -i)) != se.end()){
			cout<<v[i]<<" "<<i<<endl;
			for(auto x: se){
				cout<<x.fi<<" "<<-x.se<<endl;
			}
			return;
		}
		int p = i;
		while(w[p].fi != -1){
			auto it = se.find(w[p]);
			if(it == se.end()){
				w[p].fi = -1;
				p = -w[p].se;
				continue;
			}
			auto it2 = it;
			it2--;
			if(-it2->se < -it->se) break;
			w[p].fi = -1;
			// cout<<i<<" "<<v[i]<<" "<<"erase"<<" "<< -it->se << " " << it->fi<<endl;
			se.erase(it);
			break;
		}
		continue;
		// if(w[i].fi == -1) continue;
		// if(w[i].se == -199){
		// 	cout<<"hoge "<<v[i]<<" "<<i<<endl;
		// }
		auto it = se.find(w[i]);
		if(it == se.end()) continue;
		auto it2 = it;
		assert(it2 != se.begin());
		it2--;
		// if(w[i].se == -199){
		// 	cout<<"hoge "<<it2->fi<<" "<< -it2->se <<endl;
		// }
		// assert(it2 == se.begin());
		if(it2 != se.begin()){
			cout<<v[i]<<" "<<i<<endl;
			cout<<w[i].fi<<" "<<-w[i].se<<endl;
			cout << it2->fi << " " << -it2->se << endl;
			cout << se.begin()->fi << " " << -se.begin()->se << endl;
			for(auto x: se){
				cout<<x.fi<<" "<<-x.se<<endl;
			}
			cout<<"hoge"<<endl;
			return;
		}
		if(-it2->se < -it->se){
			w[-it2->se] = w[i];
			continue;
		}
		// cout<<"next "<< it->fi << " " << -it->se << endl;
		se.erase(it);
	}
	// cout<<m<<endl;
	// for(auto x: se){
	// 	cout<<x.fi<<" "<<-x.se<<endl;
	// }
	cout<<se.size()-1<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

Measured memory_bytes: 8572