Problem:
Score : 2400 points

Problem Statement

Takahashi loves sorting.

He has a permutation (p_1,p_2,...,p_N) of the integers from 1 through N. Now, he will repeat the following operation until the permutation becomes (1,2,...,N):

First, we will define high and low elements in the permutation, as follows. The i-th element in the permutation is high if the maximum element between the 1-st and i-th elements, inclusive, is the i-th element itself, and otherwise the i-th element is low.

Then, let a_1,a_2,...,a_k be the values of the high elements, and b_1,b_2,...,b_{N-k} be the values of the low elements in the current permutation, in the order they appear in it.

Lastly, rearrange the permutation into (b_1,b_2,...,b_{N-k},a_1,a_2,...,a_k).

How many operations are necessary until the permutation is sorted?

Constraints

1 ≤ N ≤ 2×10^5

(p_1,p_2,...,p_N) is a permutation of the integers from 1 through N.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print the number of operations that are necessary until the permutation is sorted.

Sample Input 1

5
3 5 1 2 4

Sample Output 1

3

The initial permutation is (3,5,1,2,4), and each operation changes it as follows:

In the first operation, the 1-st and 2-nd elements are high, and the 3-rd, 4-th and 5-th are low. The permutation becomes: (1,2,4,3,5).

In the second operation, the 1-st, 2-nd, 3-rd and 5-th elements are high, and the 4-th is low. The permutation becomes: (3,1,2,4,5).

In the third operation, the 1-st, 4-th and 5-th elements are high, and the 2-nd and 3-rd and 5-th are low. The permutation becomes: (1,2,3,4,5).

Sample Input 2

5
5 4 3 2 1

Sample Output 2

4

Sample Input 3

10
2 10 5 7 3 6 4 9 8 1

Sample Output 3

6

Solution in C++ (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int n,cnt,num[200100],rt,res;
struct node{
	int ch[2],first,mx,rad,val;
	bool mon;
}t[200100];
inline void pushup(const register int &x,const register bool tp=0){
	if(!tp){
		t[x].first=(lson?t[lson].first:t[x].val);
		t[x].mx=t[x].val;
		t[x].mon=true;
		if(lson)t[x].mon&=(t[lson].mon&&t[lson].mx<t[x].val),t[x].mx=max(t[x].mx,t[lson].mx);
		if(rson)t[x].mon&=(t[rson].mon&&t[rson].first>t[x].val),t[x].mx=max(t[x].mx,t[rson].mx);
	}else{
		t[x].first=(lson?t[lson].first:t[x].val);
		t[x].mx=(rson?t[rson].mx:t[x].val);
		t[x].mon=true;
	}
}
inline void build(register int &x,const register int &l,const register int &r){
	int mid=(l+r)>>1;
	x=++cnt,t[x].first=t[x].mx=t[x].val=num[mid],t[x].rad=cnt,t[x].mon=true;
	if(mid>l)build(lson,l,mid-1);
	if(mid<r)build(rson,mid+1,r);
	pushup(x);
}
inline int merge(const register int &x,const register int &y,const register bool tp=0){
	if(!x||!y)return x+y;
	if(t[x].rad>t[y].rad){t[x].ch[1]=merge(t[x].ch[1],y,tp),pushup(x,tp);return x;}
	else{t[y].ch[0]=merge(x,t[y].ch[0],tp),pushup(y,tp);return y;}
}
inline pair<int,int> split(const register int &x,register int val){
	if(!x)return mp(0,0);
	if(t[x].mx<val)return mp(0,x);
	if(t[x].mon){
		if(val<t[x].first)return mp(x,0);
		if(val<t[x].val){
			register pii p=split(lson,val);
			lson=0,pushup(x);
			p.first=merge(p.first,x,1);
			return p;
		}else{
			register pii p=split(rson,val);
			rson=0,pushup(x);
			p.second=merge(x,p.second);
			return p;
		}
	}
	register int ls=lson,rs=rson;
	lson=rson=0,pushup(x,1);
	register pii p1=split(ls,val);
	if(p1.first)val=t[p1.first].mx;
	if(t[x].val>val)p1.first=merge(p1.first,x,1),val=t[x].val;
	else p1.second=merge(p1.second,x);
	register pii p2=split(rs,val);
	return mp(merge(p1.first,p2.first,1),merge(p1.second,p2.second));
}
inline void read(register int &x){
	x=0;
	char c=getchar();
	while(c>'9'||c<'0')c=getchar();
	while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
}
inline void print(register int x){
	if(x<=9)putchar('0'+x);
	else print(x/10),putchar('0'+x%10);
}
inline void iterate(register int x){
	if(!x)return;
	iterate(lson),print(t[x].val),putchar(' '),iterate(rson);
}
int main(){
	read(n);
	for(register int i=1;i<=n;i++)read(num[i]);
	build(rt,1,n);
	while(!t[rt].mon){
//		iterate(rt);putchar('\n');
		res++;
		pii p=split(rt,0);
		rt=merge(p.second,p.first);
	}
	print(res);
	return 0;
}