#include <cstdio>

int main(){
    int N, cnt=0, len=0;
    scanf("%d", &N); long long MX[100010], tmp[100010]; len=N;
    for(int i=1; i<=N; ++i)scanf("%I64d", &MX[i]);
    while(1){
        MX[0]=-1000000000; int t=-1;
        for(int i=1; i<=len; ++i){
            if(MX[i]>=MX[i-1])
                tmp[++t]=MX[i];
        }t++;
        if(t>1 && t!=len){
            len=t; cnt++;
            for(int i=0; i<len; ++i)MX[i+1]=tmp[i];
        }
        else if(t==1){cnt++; break;}
        else break;
    }
    printf("%d\n", cnt);
    return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
#define VLL vector<long long>

int main(){
    int N, cnt=0, len=0;
    scanf("%d", &N); long long MX[100010]; len=N;
    for(int i=1; i<=N; ++i)scanf("%I64d", &MX[i]);
    while(1){
        MX[0]=-1000000000;
        VLL tmp;
        for(int i=1; i<=len; ++i){
            if(MX[i]>=MX[i-1])
                tmp.push_back(MX[i]);
        }
        if(tmp.size()>1 && tmp.size()!=len){
            len=tmp.size(); cnt++;
            for(int i=0; i<len; ++i)MX[i+1]=tmp[i];
        }
        else if(tmp.size()==1){cnt++; break;}
        else break;
    }
    printf("%d\n", cnt);
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 100005
int a[N],pre[N],next[N],v[N];
int main()
{
   // freopen("in.txt","r",stdin);
    int n,i,j;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
        {
            next[i]=i+1;pre[i+1]=i;
        }
        next[0]=0;pre[0]=0;a[0]=0;pre[1]=0;
        queue<int>q,p;
        for(i=1;i<n;i++)
        if(a[i]>a[i+1])q.push(i+1);
        memset(v,0,sizeof(v));
        int num=0;
        while(!q.empty())
        {
           num++;
           while(!q.empty())
           {
               i=q.front();q.pop();
               v[i]=1;
               next[pre[i]]=next[i];
               pre[next[i]]=pre[i];
           }
           for(i=1;i<n;i++)
           if(!v[i]&&pre[i]&&a[i]<a[pre[i]])q.push(i);
        }
        printf("%d\n",num);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cassert>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
typedef long double db;
typedef pair <int, int> pii;
typedef unsigned int usi;
const int INF = 1e9;
const int N = 1e5 + 10;
int a[N],pre[N],next[N];
int main()
{
   // freopen("in.txt","r",stdin);
    int n,m,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        next[1]=0;
        for(i=2;i<=n;i++)
        {
            next[i]=i-1;
            pre[i-1]=i;
        }
        pre[n]=n+1;
        int count=0,last=n;
        while(1)
        {
            bool ok=0;
            for(i=last;i>1;i=next[i])
            if(next[i]&&a[i]<a[next[i]])
            {
                j=pre[i];
                if(j==n+1)pre[next[i]]=n+1,last=next[i];
                else next[j]=next[i];pre[next[i]]=j;
                ok=1;
            }
            if(!ok)break;
            count++;
        }
        printf("%d\n",count);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cassert>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
typedef long double db;
typedef pair <int, int> pii;
typedef unsigned int usi;
const int INF = 1e9;
const int N = 1e5 + 10;
int a[N],pre[N],next[N],v[N];
int main()
{
   // freopen("in.txt","r",stdin);
    int n,m,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        memset(v,0,sizeof(v));
        v[0]=1;
        for(i=2;i<=n;i++)
        {
            next[i]=i-1;
            pre[i-1]=i;
        }
        pre[n]=n;
        int count=0;
        while(1)
        {
            bool ok=0;
            for(i=n;i>1;i--)
            if(!v[i]&&!v[next[i]]&&a[i]<a[next[i]])
            {
                v[i]=1;
                j=pre[i];
                next[j]=next[i];pre[next[i]]=j;
                ok=1;
            }
            if(!ok)break;
            count++;
        }
        printf("%d\n",count);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cassert>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
typedef long double db;
typedef pair <int, int> pii;
typedef unsigned int usi;
const int INF = 1e9;
const int N = 1e5 + 10;
int a[N],pre[N],next[N],v[N];
int main()
{
  //  freopen("in.txt","r",stdin);
    int n,m,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        memset(v,0,sizeof(v));
        v[0]=1;
        for(i=2;i<=n;i++)
        {
            next[i]=i-1;
            pre[i-1]=i;
        }
        pre[n]=n;
        int count=0;
        while(1)
        {
            bool ok=0;
            for(i=n;i>1;i=next[i])
            if(!v[i]&&!v[next[i]]&&a[i]<a[next[i]])
            {
                v[i]=1;
                j=pre[i];
                next[j]=next[i];pre[next[i]]=j;
                ok=1;
            }
            if(!ok)break;
            count++;
        }
        printf("%d\n",count);
    }
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 100005
int a[N],pre[N],next[N],v[N];
int main()
{
   // freopen("in.txt","r",stdin);
    int n,i,j;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
        {
            next[i]=i+1;pre[i+1]=i;
        }
        next[0]=0;pre[0]=0;a[0]=0;pre[1]=0;
        queue<int>q,p;
        for(i=1;i<n;i++)
        if(a[i]>a[i+1])q.push(i+1);
        memset(v,0,sizeof(v));
        int num=0;
        while(!q.empty())
        {
           num++;
           while(!q.empty())
           {
               i=q.front();q.pop();
               v[i]=1;
               next[pre[i]]=next[i];
               pre[next[i]]=pre[i];

           }
           for(i=1;i<n;)
           {
               j=next[i];
               if(j==0)break;
               if(!v[j]&&a[j]<a[i])q.push(j);
               i=j;
           }
        }
        printf("%d\n",num);
    }
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 100005
int a[N],next[N],v[N];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,i,j;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
        {
            next[i]=i+1;
        }
        next[0]=0;a[0]=0;next[n]=0;
        vector<int>q[2];
        memset(v,0,sizeof(v));
        for(i=n-1;i>=1;i--)
        if(a[i]>a[i+1])q[0].push_back(i),v[i+1]=1;
        int num=0,z=0;
        while(!q[z].empty())
        {
           num++;
           q[1-z].clear();
           for(int k=0;k<q[z].size();k++)
           {
               i=q[z][k];
               j=next[next[i]];
               next[i]=j;
               if(j&&!v[i]&&a[i]>a[j])v[j]=1,q[1-z].push_back(i);
           }
           z=1-z;
        }
        printf("%d\n",num);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cassert>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
typedef long double db;
typedef pair <int, int> pii;
typedef unsigned int usi;
const int INF = 1e9;
const int N = 1e5 + 10;
int a[N],pre[N],next[N],v[N];
int main()
{
   // freopen("in.txt","r",stdin);
    int n,m,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        memset(v,0,sizeof(v));
        v[0]=1;
        for(i=2;i<=n;i++)
        {
            next[i]=i-1;
            pre[i-1]=i;
        }
        pre[n]=n+1;
        int count=0,last=n;
        while(1)
        {
            bool ok=0;
            for(i=last;i>1;i=next[i])
            if(a[i]<a[next[i]])
            {
                j=pre[i];
                if(j==n+1)pre[next[i]]=n+1,last=next[i];
                else next[j]=next[i];pre[next[i]]=j;
                ok=1;
            }
            if(!ok)break;
            count++;
        }
        printf("%d\n",count);
    }
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 100005
int a[N],pre[N],next[N],v[N];
int main()
{
   // freopen("in.txt","r",stdin);
    int n,i,j;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
        {
            next[i]=i+1;pre[i+1]=i;
        }
        next[0]=0;pre[0]=0;a[0]=0;pre[1]=0;next[n]=0;
        queue<int>q,p;
        for(i=1;i<n;i++)
        if(a[i]>a[i+1])q.push(i+1);
        memset(v,0,sizeof(v));
        int num=0;
        while(!q.empty())
        {
           num++;
           while(!q.empty())
           {
               i=q.front();q.pop();
               v[i]=1;
               next[pre[i]]=next[i];
               pre[next[i]]=pre[i];

           }
           for(i=1;i<n;)
           {
               j=next[i];
               if(j==0)break;
               if(!v[j]&&a[j]<a[i])q.push(j);
               i=j;
           }
        }
        printf("%d\n",num);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
/** Constant List .. **/
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int MOD = 1000000007;
const int INF = 100000;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); //M_PI;

#define SZ(A) int(A.size())
#define PB push_back
#define ALL(A) A.begin(), A.end()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define maxn 100050

void PrintMsk(int MASK,int base,int nbit){
	for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}
void Display(int* ARR,int LEFT,int RIGHT){
	for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}
struct matrix
{
	int n; int a[25][25];
	void clear(){ memset(a,0,sizeof(a)); }
	void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }
	matrix operator * (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	matrix operator + (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];
		return c;
	}
};
/*********************main*********************/
int n,a[maxn],nxt[maxn],q[maxn],top,ans;
int work()
{
	int i,cnt=0,cur;
//	printf("\nbefore: ");
//	for ( i=0 ; i<top ; i++ ) printf("%d ",a[q[i]]);printf("\n");
	for ( i=0 ; i<top ; i++ )
	{
		cur = q[i];
		if (nxt[cur]<n && a[cur] > a[nxt[cur]])
		{
			nxt[cur] = nxt[nxt[cur]];
			q[cnt++] = cur;
		}
	}
//	printf("after: ");
//	for ( i=0 ; i<cnt ; i++ ) printf("%d ",a[q[i]]);printf("\n");
	return cnt;
}
int main()
{
	int i;
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for ( i=0 ; i<n ; i++ ) scanf("%d",&a[i]);
	for ( i=0 ; i<n ; i++ ) nxt[i] = i+1,	q[top++] = n-1-i;
	while (top=work())	ans++;
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
/** Constant List .. **/
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int MOD = 1000000007;
const int INF = 100000;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); //M_PI;

#define SZ(A) int(A.size())
#define PB push_back
#define ALL(A) A.begin(), A.end()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define maxn 100010

void PrintMsk(int MASK,int base,int nbit){
	for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}
void Display(int* ARR,int LEFT,int RIGHT){
	for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}
struct matrix
{
	int n; int a[25][25];
	void clear(){ memset(a,0,sizeof(a)); }
	void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }
	matrix operator * (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	matrix operator + (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];
		return c;
	}
};
/*********************main*********************/
int n,a[maxn],b[maxn],ans;
void dfs(int* arr,int l,int r)
{
	int top,i,tmpr;
	tmpr = r;
	top = 0;
//	for ( i=l ; i<=r ; i++ ) printf("%d ",arr[i]);	printf("\n");
	for ( i=r ; i>=l ; i-- )
	{
		if (top && arr[i]>b[top-1])	top--;
		b[top++] = arr[i];
	}
	l = 0;	r = top-1;
	if (top-1 == tmpr) return;
	ans ++;
	while (l<r)
	{
		b[l]^=b[r];	b[r]^=b[l];	b[l]^=b[r];
		l++;
		r--;
	}
	dfs(b,0,top-1);
}
int main()
{
	int i;
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for ( i=0 ; i<n ; i++ ) scanf("%d",&a[i]);
	dfs(a,0,n-1);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
/** Constant List .. **/
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int MOD = 1000000007;
const int INF = 100000;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); //M_PI;

#define SZ(A) int(A.size())
#define PB push_back
#define ALL(A) A.begin(), A.end()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define maxn 100050

void PrintMsk(int MASK,int base,int nbit){
	for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}
void Display(int* ARR,int LEFT,int RIGHT){
	for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}
struct matrix
{
	int n; int a[25][25];
	void clear(){ memset(a,0,sizeof(a)); }
	void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }
	matrix operator * (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	matrix operator + (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];
		return c;
	}
};
/*********************main*********************/
int n,a[maxn],nxt[maxn],q[maxn],top,ans,inq[maxn];
int work()
{
	int i,cnt=0,cur;
//	printf("\nbefore: ");
//	for ( i=0 ; i<top ; i++ ) printf("%d ",q[i]);printf("\n");
	for ( i=0 ; i<top ; i++ )
	{
		cur = q[i];
		inq[cur] = 0;
		if (nxt[cur]!=-1 && cur>nxt[cur])
		{
			if (inq[nxt[cur]]) cnt--;
			inq[cur] = 1;
			q[cnt++] = cur;
			nxt[cur] = nxt[nxt[cur]];
		}
	}
//	printf("after: ");
//	for ( i=0 ; i<cnt ; i++ ) printf("%d ",q[i]);printf("\n");
	return cnt;
}
int main()
{
	int i;
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	a[n] = -1;
	for ( i=0 ; i<n ; i++ ) scanf("%d",&a[i]);
	for ( i=0 ; i<n ; i++ ) nxt[a[i]] = a[i+1],	q[top++] = a[n-1-i],	inq[a[i]]=1;
	while (top=work())	ans++;
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
/** Constant List .. **/
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int MOD = 1000000007;
const int INF = 100000;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); //M_PI;

#define SZ(A) int(A.size())
#define PB push_back
#define ALL(A) A.begin(), A.end()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define maxn 100010

void PrintMsk(int MASK,int base,int nbit){
	for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}
void Display(int* ARR,int LEFT,int RIGHT){
	for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}
struct matrix
{
	int n; int a[25][25];
	void clear(){ memset(a,0,sizeof(a)); }
	void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }
	matrix operator * (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	matrix operator + (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];
		return c;
	}
};
/*********************main*********************/
int n,a[maxn];
int q[maxn];
int cnt,ans;
int newcnt()
{
	int i,l;
//	printf("before: ");
//	for ( i=0 ; i<n ; i++ ) printf("%d ",q[i]);	printf("\n");
	for ( i=l=cnt=0 ; i<n-1 ; i++ )
	{
		if (q[i]<q[i+1])
		{
			q[cnt++] = q[l];
			l = i+1;
		}
	}q[cnt++] = q[l];
//	printf("after: ");
//	for ( i=0 ; i<cnt ; i++ ) printf("%d ",q[i]);	printf("\n");
	return cnt;
}
int main()
{
	int i;
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for ( i=0 ; i<n ; i++ ) scanf("%d",&q[i]);
	while (newcnt() != n)	ans++,	n=cnt;
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
/** Constant List .. **/
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int MOD = 1000000007;
const int INF = 100000;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); //M_PI;

#define SZ(A) int(A.size())
#define PB push_back
#define ALL(A) A.begin(), A.end()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define maxn 100010

void PrintMsk(int MASK,int base,int nbit){
	for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}
void Display(int* ARR,int LEFT,int RIGHT){
	for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}
struct matrix
{
	int n; int a[25][25];
	void clear(){ memset(a,0,sizeof(a)); }
	void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }
	matrix operator * (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	matrix operator + (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];
		return c;
	}
};
/*********************main*********************/
int n,a[maxn];
int getvar(int l,int r)
{
	int i,res=0;
	for ( i=l+1 ; i<r ; i++ )
	if (a[i-1]>a[i] && a[i]<a[i+1])	res++;
	return res;
}
int dfs(int cur)
{
	int i,tmp,res;
	if (cur==n)	return 0;
	for ( i=cur+1 ; i<n ; i++ )
	if (a[i]>a[cur])	break;
	res = getvar(cur,i);
	tmp = dfs(i);
	res = max(res,tmp);
	return res;
}
int main()
{
	int i;
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for ( i=0 ; i<n ; i++ ) scanf("%d",&a[i]);
	a[n] = INF;
	int ans = dfs(0);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
/** Constant List .. **/
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};

const int MOD = 1000000007;
const int INF = 100000;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); //M_PI;

#define SZ(A) int(A.size())
#define PB push_back
#define ALL(A) A.begin(), A.end()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define maxn 100010

void PrintMsk(int MASK,int base,int nbit){
	for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}
void Display(int* ARR,int LEFT,int RIGHT){
	for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}
struct matrix
{
	int n; int a[25][25];
	void clear(){ memset(a,0,sizeof(a)); }
	void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }
	matrix operator * (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			c.a[i][j]=0;
			for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];
		}
		return c;
	}
	matrix operator + (matrix b)
	{
		matrix c; c.n=n;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			c.a[i][j]=a[i][j]+b.a[i][j];
		return c;
	}
};
/*********************main*********************/
int n,a[maxn],b[maxn],ans;
void dfs(int l,int r)
{
	int top,i,tmpr;
	tmpr = r;
	top = 0;
//	for ( i=l ; i<=r ; i++ ) printf("%d ",a[i]);	printf("\n");
	for ( i=r ; i>=l ; i-- )
	{
		if (top && a[i]>b[top-1])	top--;
		b[top++] = a[i];
	}
	l = 0;	r = top-1;
	if (top-1 == tmpr) return;
	ans ++;
	while (l<r)
	{
		b[l]^=b[r];	b[r]^=b[l];	b[l]^=b[r];
		l++;
		r--;
	}
	for ( i=0 ; i<=top-1 ; i++ ) a[i] = b[i];
	dfs(0,top-1);
}
int main()
{
	int i;
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for ( i=0 ; i<n ; i++ ) scanf("%d",&a[i]);
	dfs(0,n-1);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
typedef pair<long long, long long> pii;
long long ar[MAXn], n, ans;
vector<pii> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		//long long hp = vec.size();
		while(!vec.empty() && vec.back().first < ar[i]) {
			ans = max(ans,  vec.back().second - i);
			vec.pop_back();
		}

		vec.push_back(pii(ar[i], i));
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
typedef pair<long long, long long> pii;
long long ar[MAXn], n, ans;
vector<pii> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		//long long hp = vec.size();
		while(!vec.empty() && vec.back().first < ar[i]) {
			ans = max(ans, i - vec.back().second)
			vec.pop_back();
		}

		vec.push_back(pii(ar[i], i));
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
typedef pair<long long, long long> pii;
long long ar[MAXn], n, ans, dp[MAXn];
vector<pii> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		//long long hp = vec.size();
		while(!vec.empty() && vec.back().first < ar[i]) {
			dp[i] = max(dp[i], dp[vec.back().second] + 1);
			vec.pop_back();
		}
		vec.push_back(pii(ar[i], i));
		if (vec.empty())
			dp[i] = MAXn;
		else
			ans = max(ans, dp[i]);
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
typedef pair<long long, long long> pii;
long long ar[MAXn], n, ans;
vector<pii> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		//long long hp = vec.size();
		while(!vec.empty() && vec.back().first < ar[i]) {
			ans = max(ans, i - vec.back().second);
			vec.pop_back();
		}

		vec.push_back(pii(ar[i], i));
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
typedef pair<long long, long long> pii;
long long ar[MAXn], n, ans, dp[MAXn];
vector<pii> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = 0; i < n; i++) {
		//long long hp = vec.size();
		dp[i] = 1;
		while(!vec.empty() && vec.back().first < ar[i]) {
			dp[i] = max(dp[i], dp[vec.back().second] + 1);
			vec.pop_back();
		}

		if (vec.empty())
			dp[i] = MAXn;
		else
			ans = max(ans, dp[i]);
				vec.push_back(pii(ar[i], i));
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;

long long ar[MAXn], n, ans;
vector<int> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		long long hp = vec.size();
		while(!vec.empty() && vec.back() < ar[i])
			vec.pop_back();
		ans = max(ans, (long long)hp - vec.size());
		vec.push_back(ar[i]);
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
typedef pair<long long, long long> pii;
long long ar[MAXn], n, ans. dp[MAXn];
vector<pii> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		//long long hp = vec.size();
		while(!vec.empty() && vec.back().first < ar[i]) {
			dp[i] = max(dp[i], dp[vec.back().second] + 1);
			vec.pop_back();
		}
		vec.push_back(pii(ar[i], i));
		if (vec.empty())
			dp[i] = MAXn;
		else
			ans = max(ans, dp[i]);
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;

long long ar[MAXn], n, ans;
vector<int> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		long long hp = vec.size();
		while(!vec.empty() && vec.back() < ar[i])
			vec.pop_back();
		vec.push_back(ar[i]);
		ans = max(ans, (long long)hp - vec.size() + 1);
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;

long long ar[MAXn], n, ans;
vector<int> vec;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	for (int i = n - 1; i >= 0; i--) {
		long long hp = vec.size();
		while(!vec.empty() && vec.back() < ar[i])
			vec.pop_back();
		ans = max(ans, (long long)hp - vec.size() + 1);
		vec.push_back(ar[i]);
	}
	cout << ans;
}


#include <bits/stdc++.h>

const int N = 1e5 + 5;

using namespace std;

int n,x,max1=0,f[N];
stack <int> s;

int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        f[x] = 1;
        while(s.size() && x>s.top()) /// dk nào trước && thì xét trước
        {
            f[x] = max(f[x],f[s.top()]+1);
            s.pop();
        }

        if(!s.size()) f[x] =  0;

        s.push(x);

        max1 = max(max1,f[x]);
    }

    cout<<max1;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5+5;

using namespace std;

int n,a[N];

int main()
{
    int dem = 0;
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>a[i];
        if(a[i]>a[i-1]) dem++;
    }
    if(dem==n) return cout<<0,0;

    dem=1;
    for(int i=1;i<n;++i)
        if(a[i]>a[i+1]) dem++;
    if(dem==n) return cout<<1,0;

    dem = 0;
    for(int i=2;i<=n;++i)
        if(a[i]>a[i-1]) dem++;
    cout<<dem;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5 + 5;

using namespace std;

int n,x,max1=0,f[N];
stack <int> s;

int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        f[x] = 1;
        while(x>s.top() && s.size())
        {
            f[x] = max(f[x],f[s.top()]+1);
            s.pop();
        }

        if(!s.size()) f[x] =  0;
        s.push(x);

        max1 = max(max1,f[x]);
    }

    cout<<max1;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5+5;

using namespace std;

int n,a[N],x,k=0;
// a[i]: the step number to go to i;
stack <int> s;

int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        a[x]=1;
        while(s.size()>0 && x>s.top())
        {
            a[x]=max(a[x],a[s.top()]+1);
            s.pop();
        }
        cout<<a[x]<<endl;
        if(s.size()==0) a[x] = 0;
        s.push(x);
        k = max(k,a[x]);
    }
    cout<<k;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5 + 5;

using namespace std;

int n,x,f[N],ans=0;
stack <int> s;

int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        f[x] = 1;
        while(s.size() && x > s.top())
        {
            f[x] = max(f[x],f[s.top()]+1);
            s.pop();
        }

        if(!s.size()) f[x]=0;

        s.push(x);

        ans = max(ans,f[x]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5+5;

using namespace std;

int n,a[N];

int main()
{
    int dem = 0;
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>a[i];
        if(a[i]>a[i-1]) dem++;
    }
    if(dem==n) return cout<<0,0;

    dem=1;
    for(int i=1;i<n;++i)
        if(a[i]>a[i+1]) dem++;
    if(dem==n) return cout<<n,0;

    dem = 0;
    for(int i=2;i<=n;++i)
        if(a[i]>a[i-1]) dem++;
    cout<<dem;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5+5;

using namespace std;

int n,a[N];

int main()
{
    int dem = 0;
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>a[i];
        if(a[i]>a[i-1]) dem++;
    }
    if(dem==n) return cout<<0,0;
    dem = 0;
    for(int i=2;i<=n;++i)
    {
        if(a[i]<a[i-1]) continue;
        if(a[i]>a[i-1]) dem++;
    }
    cout<<dem;
    return 0;
}


#include <bits/stdc++.h>

const int N = 1e5+5;

using namespace std;

int n,a[N],x,k=0;
// a[i]: the step number to go to i;
stack <int> s;

int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        a[x]=1;
        while(s.size()>0 && x>s.top())
        {
            a[x]=max(a[x],a[s.top()]+1);
            s.pop();
        }
        //cout<<a[x]<<endl;
        if(s.size()==0) a[x] = 0;
        s.push(x);
        k = max(k,a[x]);
    }
    cout<<k;
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n)   FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back

class RandInt {
  public:
    RandInt(int low, int high): distribution{low, high}{ }
    int operator()() { return distribution(random_engine); }

  private:
    std::default_random_engine random_engine;
    std::uniform_int_distribution<> distribution;
};

int solve(vector<int> a) {
    int ans = -1;
    for (bool upd = true; upd; ++ans) {
        upd = false;
        vector<int> b;
        REP(i, a.size()) {
            if (i == 0 || a[i] > a[i - 1]) b.push_back(a[i]);
            else upd = true;
        }
        a = b;
    }
    return ans;
}

vector<int> gen_random_vector(int n) {
    vector<int> ret;
    vector<bool> used(n + 1, false);
    RandInt rand_int(1, n);
    while (ret.size() < n) {
        int x = rand_int();
        if (!used[x]) {
            used[x] = true;
            ret.push_back(x);
        }
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];
    //a = gen_random_vector(100000);
    //for (auto &x : a) cout << " " << x; cout << endl;

    cout << solve(a) << endl;
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n)   FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    list<int> a; REP(i, n) {
        int x; cin >> x;
        a.push_back(x);
    }
    map<int, list<int>::iterator> ptr;
    for (auto it = a.begin(); it != prev(a.end()); ++it) {
        if (*it > *next(it)) {
            ptr[*it] = it;
            //cout << *it << endl;
        }
    }

    int ans = -1;
    for (bool upd = true; upd && ans <= 20; ++ans) {
        //for (auto &x : a) cout << " " << x; cout << endl;
        upd = false;
        vector<list<int>::iterator> remove;
        for (auto &p : ptr) {
            auto it = p.second;
            if (next(it) != a.end() && (*next(it) < *it)) {
                remove.push_back(next(it));
            }
        }

        for (auto &it : remove) {
            //cout << *it << endl;
            if (ptr.find(*it) != ptr.end()) {
                ptr.erase(*it);
            }
            a.erase(it);
            upd = true;
        }
    }

    cout << ans << endl;
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n)   FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    list<int> a; REP(i, n) {
        int x; cin >> x;
        a.push_back(x);
    }
    map<int, list<int>::iterator> ptr;
    for (auto it = a.begin(); it != prev(a.end()); ++it) {
        if (*it > *next(it)) {
            ptr[*it] = it;
            //cout << *it << endl;
        }
    }

    int ans = -1;
    for (bool upd = true; upd; ++ans) {
        //for (auto &x : a) cout << " " << x; cout << endl;
        upd = false;
        vector<list<int>::iterator> remove;
        for (auto &p : ptr) {
            auto it = p.second;
            if (next(it) != a.end() && (*next(it) < *it)) {
                remove.push_back(next(it));
            }
        }

        for (auto &it : remove) {
            //cout << *it << endl;
            if (ptr.find(*it) != ptr.end()) {
                ptr.erase(*it);
            }
            a.erase(it);
            upd = true;
        }
    }

    cout << ans << endl;
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n)   FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back

int solve(vector<int> a) {
    int ans = 0;
    while (true) {
        vector<int> kill(a.size());
        int n = a.size();
        for (int i = 0, j; i < n - 1; i = j) {
            j = i + 1;
            if (a[i + 1] < a[i]) {
                while (j + 1 < n && a[j + 1] > a[j] && a[i] > a[j + 1]) {
                    ++j;
                }
                kill[i] = j - i;
            }
        }
        int mn = -1;
        for (auto &k : kill) if (k != 0) {
            if (mn == -1 || k < mn)
                mn = k;
        }

        if (mn == -1) break;

        vector<bool> die(n);
        REP(i, n) if (kill[i]) REP(j, mn) die[i + 1 + j] = true;

        vector<int> b;
        REP(i, n) if (!die[i]) b.push_back(a[i]);

        ans += mn;
        a = b;
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];
    cout << solve(a) << endl;

    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n)   FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back

class RandInt {
  public:
    RandInt(int low, int high): distribution{low, high}{ }
    int operator()() { return distribution(random_engine); }

  private:
    std::default_random_engine random_engine;
    std::uniform_int_distribution<> distribution;
};

int solve_slow(vector<int> a) {
    int ans = -1;
    for (bool upd = true; upd; ++ans) {
        upd = false;
        vector<int> b;
        REP(i, a.size()) {
            if (i == 0 || a[i] > a[i - 1]) b.push_back(a[i]);
            else upd = true;
        }
        a = b;
    }
    return ans;
}

int solve(vector<int> a) {
    int ans = 0;
    while (true) {
        //for (auto &x : a) cerr << " " << x; cerr << endl;

        vector<int> kill(a.size());
        int n = a.size();
        for (int i = 0, j; i < n - 1; i = j) {
            j = i + 1;
            if (a[i + 1] < a[i]) {
                //while (j + 1 < n && a[j + 1] > a[j]) { // WA #1
                while (j + 1 < n && a[j + 1] > a[j] && a[i] > a[j + 1]) {
                    ++j;
                }
                kill[i] = j - i;
            }
        }
        int mn = -1;
        for (auto &k : kill) if (k != 0) {
            if (mn == -1 || k < mn)
                mn = k;
        }

        //for (auto &k : kill) cout << " " << k; cout << endl;
        //cout << mn << endl;
        if (mn == -1) break;

        vector<bool> die(n);
        REP(i, n) if (kill[i]) REP(j, mn) die[i + 1 + j] = true;

        vector<int> b;
        REP(i, n) if (!die[i]) b.push_back(a[i]);

        ans += mn;
        a = b;
    }
    return ans;
}

vector<int> gen_random_vector(int n) {
    vector<int> ret;
    vector<bool> used(n + 1, false);
    RandInt rand_int(1, n);
    while (ret.size() < n) {
        int x = rand_int();
        if (!used[x]) {
            used[x] = true;
            ret.push_back(x);
        }
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];

    /*
    REP(iter, 10) {
    a = gen_random_vector(10000 - iter);
    //for (auto &x : a) cout << " " << x; cout << endl;
    cout << solve(a) << " " << solve_slow(a) << endl;
    }
    */

    //cout << solve(a) << " " << solve_slow(a) << endl;
    cout << solve(a) << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;

    int arr[n];
    for(int i = 0; i < n; i++)
        cin>>arr[i];

    int ans = -1;
    stack<int> s;
    for(int i = n-1; i >= 0; i--)
    {
        int count = -1;
        while(s.size() > 0 && s.top() < arr[i])
        {
            s.pop();
            count++;
        }
        ans = max(ans, count);
        s.push(arr[i]);
    }
    cout<<ans+1<<endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
const double pi = acos(-1.0);
const int size = 200 * 1000 + 100;
const int rms = (1 << 18) - 1;
const int hrms = rms / 2;
const int inf = 1000 * 1000 * 1000;

int rmq[rms + 1];

int rss(int v, int i, int j, int lb, int rb) {
	if (lb > j || rb < i)
		return -inf;
	if (lb >= i && rb <= j)
		return rmq[v];
	return max(rss(v * 2, i, j, lb, (lb + rb) / 2), rss(v * 2 + 1, i, j, (lb + rb) / 2 + 1, rb));
}

void change(int ps, int val) {
	ps += hrms + 1;
	rmq[ps] = val;
	while (ps > 1) {
		ps /= 2;
		rmq[ps] = max(rmq[ps * 2], rmq[ps * 2 + 1]);
	}
}

int val[size];
int from[size];

int main() {
	int n;

	//freopen("problem_b.in", "r", stdin);
	//freopen("problem_b.out", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &val[i]);
	for (int i = 0; i < n; i++) {
		from[i] = rss(1, val[i], n, 1, hrms + 1);
		change(val[i] - 1, i + 1);
		from[i]--;
	}
	for (int i = 0; i <= rms; i++)
		rmq[i] = 0;
	int glans = 0;
	for (int i = 0; i < n; i++)
		if (from[i] != -1) {
			int d = rss(1, from[i] + 2, i + 1, 1, hrms + 1) + 1;
			glans = max(glans, d);
			change(i, d);
		}
	cout << glans << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;

    int arr[n];
    for(int i = 0; i < n; i++)
        cin>>arr[i];

    vector<int> v;
    v.push_back(arr[0]);
    for(int i = 1; i < n; i++)
    {
        if(arr[i] > arr[i-1])
            v.push_back(arr[i]);
    }

    if(v.size() == n)
    {
        cout<<"0\n";
        return 0;
    }

    int ans = 0;
    stack<int> s;
    for(int i = v.size() - 1; i >= 0; i--)
    {
        int count = 0;
        while(s.size() > 0 && s.top() < v[i])
        {
            s.pop();
            count++;
        }
        ans = max(ans, count);
        s.push(v[i]);
        // cout<<count<<" size : "<<s.size()<<endl;
    }
    cout<<ans + 1<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define initialize(array,size,value) for(ll i=0;i<size;i++) array[i]=value
#define couta(array,size) for(ll i=0;i<size;i++) cout<<array[i]<<" "
#define inf (long long int) 1e18
#define eps 0.000001
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN];
ll gcd(ll a,ll b);
ll palindrome(string s);
ll modexp(ll a,ll b,ll m);
void sieve();
ll ceil(ll a, ll b);
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);
vector<ll> prefix_function(string s);

vector<pll> adj[100005];
bool vis[100005];
void dfs(ll s){
    vis[s]=true;
    for(auto x: adj[s]){
        ll weigh=x.se , neigh=x.fi;
        if(vis[neigh]==false && weigh==0){
            dfs(neigh);
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin>>n;
    ll a[n];
    loop(i,0,n) cin>>a[i];
    bool imb[n];
    imb[0]=0;
    ll mx=a[0];
    loop(i,1,n){
        if(mx>a[i]) imb[i]=true;
        else imb[i]=false;
        mx=max(mx,a[i]);
    }
    ll padd[n]={0};
    ll idx=n-1;
    padd[0]=0;
    loop(i,1,n){
        if(a[i-1]<a[i]) padd[i]=padd[i-1]+1;
        else padd[i]=0;
    }
    ll ans=-1;
    loop(i,0,n){
        if(imb[i]==true){
            ans=max(ans,padd[i]);
        }
    }
    /*couta(imb,n);
    cout<<endl;
    couta(padd,n);*/
    cout<<ans+1<<endl;

}

ll gcd(ll a, ll b){
    if (a == 0)   return b;
    if(b==0)      return a;
    if (a == b)   return a;
    if (a > b)    return gcd(a%b, b);
    return gcd(a, b%a);
}

ll palindrome(string s){
    ll l = 0;
    ll h = s.length() - 1;
    while (h > l){
        if (s[l++] != s[h--]){
            return 0;
        }
    }
    return 1;
}

ll modexp(ll a, ll b, ll m){
    if (b==0)
        return 1;
    ll temp = modexp(a,b/2,m);
    temp = (temp*temp)%m;
    if (b&1)
        return (temp*(a%m))%m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
    return temp;
}

void sieve() {
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)
        spf[i] = i;
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++) {
        if (spf[i] == i) {
            for (ll j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

vl getFactorization(ll x)
{
    vl ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}

ll ceil(ll a,ll b){
    return a/b+(a%b!=0);
}

void getZarr(string str, ll Z[]){
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    for (ll i = 1; i < n; ++i){
        if (i > R){
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        } else {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

vector<ll> prefix_function(string s) {
    ll n = (ll)s.length();
    vector<ll> pi(n);
    for (ll i = 1; i < n; i++) {
        ll j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define initialize(array,size,value) for(ll i=0;i<size;i++) array[i]=value
#define couta(array,size) for(ll i=0;i<size;i++) cout<<array[i]<<" "
#define inf (long long int) 1e18
#define eps 0.000001
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN];
ll gcd(ll a,ll b);
ll palindrome(string s);
ll modexp(ll a,ll b,ll m);
void sieve();
ll ceil(ll a, ll b);
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);
vector<ll> prefix_function(string s);

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin>>n;
    ll a[n];
    loop(i,0,n) cin>>a[i];
    stack<ll> s;
    ll t[n+1];
    initialize(t,n+1,-1);
    s.push(a[0]);
    loop(i,1,n){
        //cout<<"i: "<<i<<endl;
        //cout<<s.top()<<endl;
        if(a[i]<s.top()){
            t[a[i]]=0;
            s.push(a[i]);
        }
        else if(a[i]>s.top()){
            while(s.top()<a[i]){
                //cout<<"here"<<endl;
                //cout<<t[s.top()]<<endl;
                t[a[i]]=max(t[a[i]],t[s.top()]+1);
                s.pop();
                if(s.empty()) break;
            }
            if(s.empty()){
                t[a[i]]=-1;
            }
            s.push(a[i]);
        }
        //cout<<endl;
    }
    ll ans=INT_MIN;
    loop(i,1,n+1){
        ans=max(ans,t[i]);
    }
    cout<<ans+1<<endl;
}

ll gcd(ll a, ll b){
    if (a == 0)   return b;
    if(b==0)      return a;
    if (a == b)   return a;
    if (a > b)    return gcd(a%b, b);
    return gcd(a, b%a);
}

ll palindrome(string s){
    ll l = 0;
    ll h = s.length() - 1;
    while (h > l){
        if (s[l++] != s[h--]){
            return 0;
        }
    }
    return 1;
}

ll modexp(ll a, ll b, ll m){
    if (b==0)
        return 1;
    ll temp = modexp(a,b/2,m);
    temp = (temp*temp)%m;
    if (b&1)
        return (temp*(a%m))%m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
    return temp;
}

void sieve() {
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)
        spf[i] = i;
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++) {
        if (spf[i] == i) {
            for (ll j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

vl getFactorization(ll x)
{
    vl ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}

ll ceil(ll a,ll b){
    return a/b+(a%b!=0);
}

void getZarr(string str, ll Z[]){
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    for (ll i = 1; i < n; ++i){
        if (i > R){
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        } else {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

vector<ll> prefix_function(string s) {
    ll n = (ll)s.length();
    vector<ll> pi(n);
    for (ll i = 1; i < n; i++) {
        ll j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define initialize(array,size,value) for(ll i=0;i<size;i++) array[i]=value
#define couta(array,size) for(ll i=0;i<size;i++) cout<<array[i]<<" "
#define inf (long long int) 1e18
#define eps 0.000001
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN];
ll gcd(ll a,ll b);
ll palindrome(string s);
ll modexp(ll a,ll b,ll m);
void sieve();
ll ceil(ll a, ll b);
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);
vector<ll> prefix_function(string s);

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin>>n;
    ll a[n];
    loop(i,0,n) cin>>a[i];
    stack<ll> s;
    ll t[n];
    initialize(t,n,-1);
    s.push(a[0]);
    loop(i,1,n){
        if(a[i]<s.top()){
            t[i]=0;
            s.push(a[i]);
        }
        else if(a[i]>s.top()){
            while(s.top()<a[i]){
                t[i]=max(t[i],t[s.top()]+1);
                s.pop();
                if(s.empty()) break;
            }
            if(s.empty()){
                t[i]=-1;
            }
            s.push(a[i]);
        }
    }
    ll ans=INT_MIN;
    loop(i,0,n){
        ans=max(ans,t[i]);
    }
    cout<<ans+1<<endl;
}

ll gcd(ll a, ll b){
    if (a == 0)   return b;
    if(b==0)      return a;
    if (a == b)   return a;
    if (a > b)    return gcd(a%b, b);
    return gcd(a, b%a);
}

ll palindrome(string s){
    ll l = 0;
    ll h = s.length() - 1;
    while (h > l){
        if (s[l++] != s[h--]){
            return 0;
        }
    }
    return 1;
}

ll modexp(ll a, ll b, ll m){
    if (b==0)
        return 1;
    ll temp = modexp(a,b/2,m);
    temp = (temp*temp)%m;
    if (b&1)
        return (temp*(a%m))%m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
    return temp;
}

void sieve() {
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)
        spf[i] = i;
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++) {
        if (spf[i] == i) {
            for (ll j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

vl getFactorization(ll x)
{
    vl ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}

ll ceil(ll a,ll b){
    return a/b+(a%b!=0);
}

void getZarr(string str, ll Z[]){
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    for (ll i = 1; i < n; ++i){
        if (i > R){
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        } else {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

vector<ll> prefix_function(string s) {
    ll n = (ll)s.length();
    vector<ll> pi(n);
    for (ll i = 1; i < n; i++) {
        ll j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define initialize(array,size,value) for(ll i=0;i<size;i++) array[i]=value
#define couta(array,size) for(ll i=0;i<size;i++) cout<<array[i]<<" "
#define inf (long long int) 1e18
#define eps 0.000001
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN];
ll gcd(ll a,ll b);
ll palindrome(string s);
ll modexp(ll a,ll b,ll m);
void sieve();
ll ceil(ll a, ll b);
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);
vector<ll> prefix_function(string s);

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin>>n;
    ll a[n];
    loop(i,0,n) cin>>a[i];
    stack<ll> s;
    ll t[n+1];
    initialize(t,n+1,-1);
    s.push(a[0]);
    loop(i,1,n){
        //cout<<"i: "<<i<<endl;
        //cout<<s.top()<<endl;
        if(a[i]<s.top()){
            t[a[i]]=0;
            s.push(a[i]);
        }
        else if(a[i]>s.top()){
            while(s.top()<a[i]){
                //cout<<"here"<<endl;
                //cout<<t[s.top()]<<endl;
                t[a[i]]=max(t[a[i]],t[s.top()]+1);
                s.pop();
                if(s.empty()) break;
            }
            if(s.empty()){
                t[i]=-1;
            }
            s.push(a[i]);
        }
        //cout<<endl;
    }
    ll ans=INT_MIN;
    loop(i,1,n+1){
        ans=max(ans,t[i]);
    }
    cout<<ans+1<<endl;
}

ll gcd(ll a, ll b){
    if (a == 0)   return b;
    if(b==0)      return a;
    if (a == b)   return a;
    if (a > b)    return gcd(a%b, b);
    return gcd(a, b%a);
}

ll palindrome(string s){
    ll l = 0;
    ll h = s.length() - 1;
    while (h > l){
        if (s[l++] != s[h--]){
            return 0;
        }
    }
    return 1;
}

ll modexp(ll a, ll b, ll m){
    if (b==0)
        return 1;
    ll temp = modexp(a,b/2,m);
    temp = (temp*temp)%m;
    if (b&1)
        return (temp*(a%m))%m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
    return temp;
}

void sieve() {
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)
        spf[i] = i;
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++) {
        if (spf[i] == i) {
            for (ll j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

vl getFactorization(ll x)
{
    vl ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}

ll ceil(ll a,ll b){
    return a/b+(a%b!=0);
}

void getZarr(string str, ll Z[]){
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    for (ll i = 1; i < n; ++i){
        if (i > R){
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        } else {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

vector<ll> prefix_function(string s) {
    ll n = (ll)s.length();
    vector<ll> pi(n);
    for (ll i = 1; i < n; i++) {
        ll j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}


#include <bits/stdc++.h>
#include <stdint.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define pmp make_pair
#define  M_PI    3.14159265358979323846
ll mod= 100000000000+7;

//you can't see it on my face but
//i am about to play my ace

//time gone , gone forever
//this abnormal opportunity gone, gone forever

int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    vector <int> v(n), a(n);
    for(int i=0;i<n; i++)
    cin>>v[i];
    stack <int> s;
    s.push(0);
    a[0]=-1;
    for(int i=1; i<n; i++)
    {
    	while((s.empty()==0)&&(v[s.top()]<v[i]))
    	{s.pop();
		}
		if(s.empty()==1) a[i]=-1;
		else a[i]=s.top();
		s.push(i);
	}
	int ans=0;
    for(int i=0; i<n; i++)
    {   //cout<<a[i]<<" ";
	    if(a[i]!=-1)
	    {
	    	int temp=i-a[i];
	    	ans=max(temp, ans);
		}
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
#include <stdint.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define pmp make_pair
#define  M_PI    3.14159265358979323846
ll mod= 100000000000+7;

//you can't see it on my face but
//i am about to play my ace

//time gone , gone forever
//this abnormal opportunity gone, gone forever

int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    vector <int> v(n), a(n);
    for(int i=0;i<n; i++)
    cin>>v[i];
    stack <int> s;
    s.push(0);
    a[0]=-1;
    for(int i=1; i<n; i++)
    {
    	while((s.empty()==0)&&(v[s.top()]<v[i]))
    	{s.pop();
		}
		if(s.empty()==1) a[i]=-1;
		else a[i]=s.top();
		s.push(i);
	}
    vector <int> ans(n, 0);
    ans[0]=0;
    int out=0;
    for(int i=1; i<n; i++)
    {   //cout<<a[i]<<" ";
	    if(a[i]!=-1)
	    {
	    	int temp=i-a[i], maxx=0;
	    	for(int j=a[i]+1; j<i; j++)
	    	maxx=max(ans[j], maxx);
	    	ans[i]=min(maxx+1, temp);
	    	out=max(out, ans[i]);
		}
	}
	cout<<out;
	return 0;
}


#include <bits/stdc++.h>
#include <stdint.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define pmp make_pair
#define  M_PI    3.14159265358979323846
ll mod= 100000000000+7;

//you can't see it on my face but
//i am about to play my ace

//time gone , gone forever
//this abnormal opportunity gone, gone forever

int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    vector <int> v(n), a(n);
    for(int i=0;i<n; i++)
    cin>>v[i];
    stack <int> s;
    a[n-1]=0;
    s.push(v[n-1]);
    int ans=0;
    for(int i=n-2; i>=0; i--)
    {
    	int c=0;
    	if(v[i]<s.top())
    	{
    		a[i]=0;
    		s.push(v[i]);
		}
        else
        {
        	a[i]=max(1, a[i+1]);
        	s.pop();
    	    while((s.empty()==0)&&(s.top()<v[i]))
    	    {s.pop(); c++;
			}
			s.push(v[i]);
		    a[i]+=c;
		}

    	ans=max(a[i], ans);
	}
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <stack>

using namespace std;

const int MAX_N = 100010;
const int INF = 1000000;

int n;
int values[MAX_N], life[MAX_N];
stack<int> killers;

int main() {
#ifndef ONLINE_JUDGE
  freopen("b.in", "r", stdin);
#endif

  scanf("%d ", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d ", &values[i]);
  }

  life[0] = INF;
  killers.push(0);
  for (int i = 1; i <= n; ++i) {
    life[i] = 1;
    while (killers.size() && values[i] > values[killers.top()]) {
      life[i] = max(life[i], life[killers.top()] + 1);
      killers.pop();
    }
    killers.push(i);
  }

  int sol = 0;
  for (int i = 1; i <= n; ++i) {
    if (life[i] < INF) {
      sol = max(sol, life[i]);
    }
  }

  printf("%d\n", sol);

  return 0;
}


#include <bits/stdc++.h>
#include <stdint.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define pmp make_pair
#define  M_PI    3.14159265358979323846
ll mod= 100000000000+7;

//you can't see it on my face but
//i am about to play my ace

//time gone , gone forever
//this abnormal opportunity gone, gone forever

int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    vector <int> v(n), a(n);
    for(int i=0;i<n; i++)
    cin>>v[i];
    stack <int> s;
    s.push(0);
    a[0]=-1;
    vector <int> ans(n, 0);
    ans[0]=0;
    int out=0;
    for(int i=1; i<n; i++)
    {   int maxx=0;
    	while((s.empty()==0)&&(v[s.top()]<v[i]))
    	{  maxx=max(ans[s.top()], maxx);
		   s.pop();
		}
		if(s.empty()==1) a[i]=-1;
		else a[i]=s.top();
		s.push(i);

		 if(a[i]!=-1)
	    {
	    	int temp=i-a[i];
	    	ans[i]=min(maxx+1, temp);
	    	out=max(out, ans[i]);
		}
	}

	cout<<out;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

stack<pair<int, int> > st;
int n, res, t, a[N], d[N];

void Input()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
}

void Solve()
{
    st.push(make_pair(a[1], 1));
    for (int i = 2; i <= n; i++)
    {
        while (!st.empty() && a[i] >= st.top().first) st.pop();
        if (!st.empty()) d[i] = i - st.top().second;
        st.push(make_pair(a[i], i));
    }

    for (int i = 2; i <= n; i++)
    {
        if (d[i] == 1) t = 1;
        if (d[i - 1] < d[i])
            res++;
    }

    res = max(res - 1, 0);
    if (!res) res = t;
    cout << res << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    Input();
    Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int n, res, t, a[N], d[N], tree[4 * N];
stack<pair<int, int> > st;

void Input()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        d[i] = i;
    }
}

void Update(int k, int l, int r, int idx, int val)
{
    if (l > idx || r < idx) return;

    if (l == r)
    {
        tree[k] = val;
        return;
    }

    int mid = (l + r) / 2;
    Update(k * 2, l, mid, idx, val);
    Update(k * 2 + 1, mid + 1, r, idx, val);

    tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}

int Get(int k, int l, int r, int L, int R)
{
    if (l > R || r < L) return 0;

    if (L <= l && r <= R) return tree[k];

    int mid = (l + r) / 2;
    return (max(Get(k * 2, l, mid, L, R), Get(k * 2 + 1, mid + 1, r, L, R)));
}

void Solve()
{
    st.push(make_pair(a[1], 1));
    for (int i = 2; i <= n; i++)
    {
        while (!st.empty() && a[i] >= st.top().first) st.pop();
        if (!st.empty()) d[i] = st.top().second;
        st.push(make_pair(a[i], i));
    }

    for (int i = 1; i <= n; i++)
    {
        if (d[i] == i) continue;

        int curr = Get(1, 1, n, d[i] + 1, i) + 1;
        if (d[i] + 1 == i) curr = 1;
        Update(1, 1, n, i, curr);
        res = max(res, curr);
    }

    cout << res << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    Input();
    Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int n, res, c, a[N];
bool check[N];

void Input()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
}

void Solve()
{
    while (true)
    {
        int cnt = 0;
        for (int i = n - 1; i >= 1; i--)
            if (a[i + 1] <= a[i]) c = 1, check[i + 1] = true;

        if (!c) break;

        for (int i = 1; i <= n; i++)
        {
            if (!check[i]) cnt++, a[cnt] = a[i];
            check[i] = false;
        }

        res++, c = 0, n = cnt;
    }

    cout << res << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    Input();
    Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

stack<pair<int, int> > st;
int n, res, a[N], d[N];

void Input()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
}

void Solve()
{
    st.push(make_pair(a[1], 1));
    for (int i = 2; i <= n; i++)
    {
        while (!st.empty() && a[i] >= st.top().first) st.pop();
        if (!st.empty()) d[i] = i - st.top().second;
        st.push(make_pair(a[i], i));
    }

    for (int i = 2; i <= n; i++)
    {
        if (d[i - 1] < d[i])
            res++;
    }

    cout << max(0, res - 1) << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    Input();
    Solve();
    return 0;
}


/********************************
*        GGGGG      GGGGG       *
*       G          G            *
*       G  GGG     G  GGG       *
*       G   GG     G   GG       *
*        GGGGG      GGGGG       *
*                               *
********************************/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define trace(x) for(auto it:x)
#define loop(a,n) for(int i = a; i < n; i++)
#define loop2(a,n) for(int j = a; j < n; j++)
#define ms(arr, x) memset(&arr, x, sizeof(arr))
#define err(x) cout << x << endl
using namespace std;

const int MAX = 1e5   + 5;
const int MOD = 1e9 + 7;
const int inf = 1e18;
const int bsz = 700;
int res[MAX];

unordered_map<ll,ll>frq;
ll ans = 0;
struct node{

    int l, r;
    int id;

}q[MAX];

bool cmp(const node &x, const node &y){
    if(x.l/bsz == y.l/bsz) return (((x.l/bsz)&1)?x.r<y.r:x.r>y.r);
	else return (x.l/bsz<y.l/bsz);
}

inline void add(int x)
{
 //  if(x == -1)return;
    if(frq[x] == 0)ans++;

    frq[x]++;

}

inline void sub(int x)
{

//   if(x == -1)return;
    frq[x]--;
    if(frq[x] == 0)ans--;
}
ll printNGE(int arr[], int n) {
  stack < pair<int,int> > s;

  s.push({arr[0],0});
  ll xx = 0;
  for (int i = 1; i < n; i++) {

    ll cnt = 0;
    if (s.empty()) {
      s.push({arr[i],i});
      continue;
    }
    while (s.empty() == false && s.top().first < arr[i]){
        cnt++;
        s.pop();
    }

    xx = max(cnt, xx);

    s.push({arr[i],i});
  }

  while (s.empty() == false) {
    s.pop();
  }

    return xx;

}
int main()
{

    ll n;
    cin >> n;

    int arr[n];

    loop(0, n)cin >> arr[i];

    reverse(arr, arr + n);
    err(printNGE(arr, n));

    return 0;
}
/*

*/


/********************************
*        GGGGG      GGGGG       *
*       G          G            *
*       G  GGG     G  GGG       *
*       G   GG     G   GG       *
*        GGGGG      GGGGG       *
*                               *
********************************/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define trace(x) for(auto it:x)
#define loop(a,n) for(int i = a; i < n; i++)
#define loop2(a,n) for(int j = a; j < n; j++)
#define ms(arr, x) memset(&arr, x, sizeof(arr))
#define err(x) cout << x << endl
using namespace std;

const int MAX = 1e5   + 5;
const int MOD = 1e9 + 7;
const int inf = 1e18;

ll printNGE(int arr[], int n) {
  stack < pair<int,int> > s;
  int ans = 0;
  s.push({arr[0],0});

  // iterate for rest of the elements
  for (int i = 1; i < n; i++) {

    if (s.empty()) {
      s.push({arr[i],i});
      continue;
    }

    /* if stack is not empty, then
       pop an element from stack.
       If the popped element is smaller
       than next, then
    a) print the pair
    b) keep popping while elements are
    smaller and stack is not empty */
    while (s.empty() == false && s.top().first < arr[i])
    {
        ans = max(ans, abs(s.top().second - i));
        s.pop();
    }

    s.push({arr[i],i});
  }

  /* After iterating over the loop, the remaining
  elements in stack do not have the next greater
  element, so print -1 for them */
  while (s.empty() == false) {
    s.pop();
  }

  return ans;
}
int main()
{

    ll n;
    cin >> n;

    int arr[n];

    loop(0, n)cin >> arr[i];

    reverse(arr, arr + n);
    cout << printNGE(arr, n);
    return 0;
}
/*

*/


/********************************
*        GGGGG      GGGGG       *
*       G          G            *
*       G  GGG     G  GGG       *
*       G   GG     G   GG       *
*        GGGGG      GGGGG       *
*                               *
********************************/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define trace(x) for(auto it:x)
#define loop(a,n) for(int i = a; i < n; i++)
#define loop2(a,n) for(int j = a; j < n; j++)
#define ms(arr, x) memset(&arr, x, sizeof(arr))
#define err(x) cout << x << endl
using namespace std;

const int MAX = 1e5   + 5;
const int MOD = 1e9 + 7;
const int inf = 1e18;
const int bsz = 700;
int res[MAX];

unordered_map<ll,ll>frq;
ll ans = 0;
struct node{

    int l, r;
    int id;

}q[MAX];

bool cmp(const node &x, const node &y){
    if(x.l/bsz == y.l/bsz) return (((x.l/bsz)&1)?x.r<y.r:x.r>y.r);
	else return (x.l/bsz<y.l/bsz);
}

inline void add(int x)
{
 //  if(x == -1)return;
    if(frq[x] == 0)ans++;

    frq[x]++;

}

inline void sub(int x)
{

//   if(x == -1)return;
    frq[x]--;
    if(frq[x] == 0)ans--;
}
ll printNGE(int arr[], int n) {
  stack < pair<int,int> > s;

  s.push({arr[0],0});
  ll xx = 0;
  ll fin[n] = {};
  // iterate for rest of the elements
  for (int i = 1; i < n; i++) {

    if (s.empty()) {
      s.push({arr[i],i});
      continue;
    }

    /* if stack is not empty, then
       pop an element from stack.
       If the popped element is smaller
       than next, then
    a) print the pair
    b) keep popping while elements are
    smaller and stack is not empty */
    while (s.empty() == false && s.top().first < arr[i])
    {
        fin[s.top().second] = abs(s.top().second - i);
        s.pop();
    }

    s.push({arr[i],i});
  }

  /* After iterating over the loop, the remaining
  elements in stack do not have the next greater
  element, so print -1 for them */
  while (s.empty() == false) {
    fin[s.top().second] = -1;
    s.pop();
  }
  reverse(fin, fin + n);
  loop(0, n){

    q[i].l = i - fin[i];
    q[i].r = i;
    q[i].id = i;
  }

  sort(q, q + n, cmp);

    int l = 0, r = -1;

    for(int i = 0; i < n; i++){

        if(q[i].l > q[i].r)continue;
        q[i].l++;
        while(l < q[i].l){

            sub(fin[l++]);
        }

        while(l > q[i].l){
            add(fin[--l]);

        }

        while(r > q[i].r){
                sub(fin[r--]);
        }

        while(r < q[i].r){

            add(fin[++r]);

        }
     //   cout << q[i].l << " " << q[i].r << " "<< ans<< endl;
        xx = max(ans, xx);

    }

    return xx;

}
int main()
{

    ll n;
    cin >> n;

    int arr[n];

    loop(0, n)cin >> arr[i];

    reverse(arr, arr + n);
    err(printNGE(arr, n));

    return 0;
}
/*

*/


/********************************
*        GGGGG      GGGGG       *
*       G          G            *
*       G  GGG     G  GGG       *
*       G   GG     G   GG       *
*        GGGGG      GGGGG       *
*                               *
********************************/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define trace(x) for(auto it:x)
#define loop(a,n) for(int i = a; i < n; i++)
#define loop2(a,n) for(int j = a; j < n; j++)
#define ms(arr, x) memset(&arr, x, sizeof(arr))
#define err(x) cout << x << endl
using namespace std;

const int MAX = 1e5   + 5;
const int MOD = 1e9 + 7;
const int inf = 1e18;

ll printNGE(int arr[], int n) {
  stack < pair<int,int> > s;
  int ans = 0;
  s.push({arr[0],0});

  ll fin[n + 1] = {};
  // iterate for rest of the elements
  for (int i = 1; i < n; i++) {

    if (s.empty()) {
      s.push({arr[i],i});
      continue;
    }

    /* if stack is not empty, then
       pop an element from stack.
       If the popped element is smaller
       than next, then
    a) print the pair
    b) keep popping while elements are
    smaller and stack is not empty */
    while (s.empty() == false && s.top().first < arr[i])
    {
        fin[s.top().second] = abs(s.top().second - i);
        s.pop();
    }

    s.push({arr[i],i});
  }

  /* After iterating over the loop, the remaining
  elements in stack do not have the next greater
  element, so print -1 for them */
  while (s.empty() == false) {
    fin[s.top().second] = -1;
    s.pop();
  }
  set<int>ss;
  loop(0, n)ss.insert(fin[i]);

  return ss.size();
}
int main()
{

    ll n;
    cin >> n;

    int arr[n];

    loop(0, n)cin >> arr[i];

    reverse(arr, arr + n);
    cout << printNGE(arr, n) - 1;
    return 0;
}
/*

*/


/********************************
*        GGGGG      GGGGG       *
*       G          G            *
*       G  GGG     G  GGG       *
*       G   GG     G   GG       *
*        GGGGG      GGGGG       *
*                               *
********************************/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define trace(x) for(auto it:x)
#define loop(a,n) for(int i = a; i < n; i++)
#define loop2(a,n) for(int j = a; j < n; j++)
#define ms(arr, x) memset(&arr, x, sizeof(arr))
#define err(x) cout << x << endl
using namespace std;

const int MAX = 1e5   + 5;
const int MOD = 1e9 + 7;
const int inf = 1e18;
const int bsz = 700;

int fw[2 * MAX];
ll getSum(int i){
   ll sum = 0;
   while(i > 0)
   {
      sum += fw[i];
      i -= i & (-i);
   }
   return sum;
}
void updateFW(int n, int i, int newVal)
{
   while (i <= n)
   {
      fw[i] += newVal;

      i += i & (-i);
   }
}
ll printNGE(int arr[], int n) {
  stack < pair<int,int> > s;

  ll fin[n] = {}, res[n];
  loop(0, n)res[i] = 1;
  s.push({arr[0], 0});
  for (int i = 1; i < n; i++){

    if (s.empty()) {
      s.push({arr[i],i});
      continue;
    }

    while (s.empty() == false && s.top().first < arr[i]){

        fin[s.top().second] = i;
        s.pop();
    }

    s.push({arr[i],i});
  }

  while (s.empty() == false) {
    fin[s.top().second] = -1;
    s.pop();
  }

  ll cmx = arr[0];
  res[0] = 0;
  loop(1, n){

      if(cmx > arr[i]){
            if(cmx > arr[fin[i]])res[fin[i]] = max(res[fin[i]], res[i] + 1);
      }else{
        res[i] = 0;
        cmx = arr[i];
      }
  }

  //loop(0, n)cout << res[i] << " ";
    ll mx = 0;
    loop(0, n)mx = max(res[i], mx);

    return mx;

}
int main()
{

    ll n;
    cin >> n;

    int arr[n];

    loop(0, n)cin >> arr[i];

    err(printNGE(arr, n));

    return 0;
}
/*

*/


/********************************
*        GGGGG      GGGGG       *
*       G          G            *
*       G  GGG     G  GGG       *
*       G   GG     G   GG       *
*        GGGGG      GGGGG       *
*                               *
********************************/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define trace(x) for(auto it:x)
#define loop(a,n) for(int i = a; i < n; i++)
#define loop2(a,n) for(int j = a; j < n; j++)
#define ms(arr, x) memset(&arr, x, sizeof(arr))
#define err(x) cout << x << endl
using namespace std;

const int MAX = 1e5   + 5;
const int MOD = 1e9 + 7;
const int inf = 1e18;
const int bsz = 700;

int fw[2 * MAX];
ll getSum(int i){
   ll sum = 0;
   while(i > 0)
   {
      sum += fw[i];
      i -= i & (-i);
   }
   return sum;
}
void updateFW(int n, int i, int newVal)
{
   while (i <= n)
   {
      fw[i] += newVal;

      i += i & (-i);
   }
}
ll printNGE(int arr[], int n) {
  stack < pair<int,int> > s;

  ll fin[n] = {}, res[n];
  loop(0, n)res[i] = 1;
  s.push({arr[0], 0});
  for (int i = 1; i < n; i++){

    if (s.empty()) {
      s.push({arr[i],i});
      continue;
    }

    while (s.empty() == false && s.top().first < arr[i]){

        fin[s.top().second] = i;
        s.pop();
    }

    s.push({arr[i],i});
  }

  while (s.empty() == false) {
    fin[s.top().second] = -1;
    s.pop();
  }

  ll cmx = arr[0];
  loop(1, n){

      if(cmx > arr[i]){
            if(cmx > arr[fin[i]])res[fin[i]] = max(res[fin[i]], res[i] + 1);
      }else{
        res[i] = 0;
        cmx = arr[i];
      }
  }

  //loop(0, n)cout << res[i] << " ";
    ll mx = 0;
    loop(0, n)mx = max(res[i], mx);

    return mx;

}
int main()
{

    ll n;
    cin >> n;

    int arr[n];

    loop(0, n)cin >> arr[i];

    err(printNGE(arr, n));

    return 0;
}
/*

*/


#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;

const double PI=3.14159265359;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;

    vector<int> v(n);
    for(int&i:v)cin>>i;

    if(is_sorted(v.begin(),v.end())){
        cout<<0;
        return 0;
    }

    vector<int> v2;
    int ct=0;
    while(1){
        for(int i=0; i<v.size(); i++){
            if(!i || v[i]>v[i-1])
                v2.push_back(v[i]);
        }
        if(v2.size()==v.size()) break;

        ct++;
        v=v2;
        v2.clear();
    }
    cout<<ct;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;

const double PI=3.14159265359;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;

    vector<int> v(n);
    for(int&i:v)cin>>i;

    if(is_sorted(v.begin(),v.end())){
        cout<<0;
        return 0;
    }

    vector<int> v2;
    int ct=0;
    while(v.size()>1){
        for(int i=0; i<v.size(); i++){
            int rv=(i==n-1?0:v[i+1]), lv=(i?v[i-1]:0);
            if(v[i]>max(lv,rv))
                v2.push_back(v[i]);
        }

        ct++;
        v=v2;

        v2.clear();
    }
    cout<<ct;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;
const double PI=3.14159265359;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int> v(n), lpos(n,-1);
    for(int&i:v)cin>>i;

    stack<int> s;

    for(int i=0; i<n; i++){
        while(s.size() && v[s.top()]<v[i]){
            s.pop();
        }
        if(s.size()){
            lpos[i]=s.top();
        }
        s.push(i);
    }
    int ans=0;

    for(int i=0; i<n; i++){
        if(lpos[i]!=-1){
            ans=max(ans,i-lpos[i]);
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;

const double PI=3.14159265359;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int> v(n), life(n,MOD);
    for(int&i:v)cin>>i;

    stack<int> s;
    s.push(0);
    for(int i=1; i<n; i++){
        life[i]=1;
        while(s.size() && v[s.top()]<v[i]){
            life[i]=max(life[i], life[s.top()]+1);
            s.pop();
        }
        s.push(i);
    }
    int ans=0;

    for(int i=0; i<n; i++){
        if(life[i]<MOD){
            ans=max(ans,life[i]);
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD=1e9+7;

const double PI=3.14159265359;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int> v(n), life(n,MOD);
    for(int&i:v)cin>>i;

    stack<int> s;

    for(int i=0; i<n; i++){
        life[i]=1;
        while(s.size() && v[s.top()]<v[i]){
            life[i]=max(life[i], life[s.top()]+1);
            s.pop();
        }
        s.push(i);
    }
    int ans=0;

    for(int i=0; i<n; i++){
        if(life[i]!=-1){
            ans=max(ans,life[i]);
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
//#else
//#define dbg(args...)

int a[100005];
stack <int> st;

int main()
{
    optimize();
    int n;
    cin >> n;
    for( int i = 0; i < n; ++i ) cin >> a[i];
    int extra = 0, ans = 0;
    for( int i = n-1; i > 0; --i ) {
        if( a[i-1] > a[i] ) {
            continue;
        }
        else {
            int cnt = 0;
            while( !st.empty() ) {
                int t = st.top();
                if( a[i] > t ) {
                    ++cnt;
                    st.pop();
                }
                else break;
            }
            extra = max(extra, cnt);
            st.push(a[i]);
        }
    }
    int cnt = 0;
    while( !st.empty() ) {
        int t = st.top();
        if( a[0] > t ) {
            ++cnt;
            st.pop();
        }
        else break;
    }
    extra = max(extra, cnt);
    if( !extra && n > 1 && a[0] > a[1] ) ans = 1;
    else if( !extra && n > 1 && a[0] < a[1] ) ans = 0;
    else ans = extra + 1;
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
//#else
//#define dbg(args...)

int a[100005];
stack <int> st;

int main()
{
    optimize();
    int n;
    cin >> n;
    for( int i = 0; i < n; ++i ) cin >> a[i];
    int extra = 0, ans = 0;
    for( int i = n-1; i > 0; --i ) {
        if( a[i-1] > a[i] ) {
            int cnt = 0;
            while( !st.empty() ) {
                int t = st.top();
                if( a[i-1] > t ) {
                    ++cnt;
                    st.pop();
                }
                else break;
            }
            extra = max(extra, cnt);
        }
        else {
            st.push(a[i]);
        }
    }
    if( !extra && n > 1 && a[0] > a[1] ) ans = 1;
    else if( !extra && n > 1 && a[0] < a[1] ) ans = 0;
    else ans = extra + 1;
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
//#else
//#define dbg(args...)

const int mx = 1e5 + 5;
int a[mx];

int main()
{
    optimize();
    int n;
    cin >> n;
    for( int i = 0; i < n; ++i ) cin >> a[i];
    int b = a[0], ans = 0, extra = 0, temp = 0;
    bool ok = 0;
    for( int i = 0; i < n-1; ++i ) {
        if( a[i] > a[i+1] ) {
            temp = 0;
        }
        else {
            if( a[i+1] > b ) {
                b = a[i+1];
                temp = 0;
            }
            else {
                ++temp;
                extra = max(extra, temp);
            }
        }
    }
    if( !extra ) {
        if( a[0] < a[1] ) ans = 0;
        else ans = 1;
    }
    else ans = 1+extra;
    cout << ans;
}