/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/
//use readint!
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include<map>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stdio.h>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<string.h>
#define mp make_pair
#define ll long long
#define all(v) v.begin(),v.end()
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
inline int pt(int a[],int l,int r){
	int p,i,j;
	p=a[l];
	i=l;
	j=r+1;
	for(;;){
		while(a[++i]<p) if(i>=r) break;
		while(a[--j]>p) if(j<=l) break;
		if(i>=j) break;
		else swap(a[i],a[j]);
	}
	if(j==l) return j;
	swap(a[l],a[j]);
	return j;
}
inline void q_sort(int a[],int l,int r){
	int q;
	if(r>l){
		q=pt(a,l,r);
		q_sort(a,l,q-1);
		q_sort(a,q+1,r);
	}
}
inline void rd(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline void quickSort(int s[],int l,int r)  {
    if(l<r){
        int i=l,j=r,x=s[l];
        while(i<j){
            while(i<j&&s[j]>=x) j--;
            if(i<j) s[i++]=s[j];
            while(i<j&&s[i]<x) i++;
            if(i<j) s[j--] = s[i];
        }
        s[i]=x;
        quickSort(s, l, i - 1);
        quickSort(s, i + 1, r);
    }
}
int n,T;
int tr[400005];
int gcd[400005];
int zh[400005];
vector<pair<int,int> > v;
inline void btr(int x,int l,int r){
	if(l==r){
		gcd[x]=zh[l];
		return;
	}
	btr(x*2,l,(l+r)/2);
	btr(x*2+1,(l+r)/2+1,r);
	gcd[x]=__gcd(gcd[x*2],gcd[x*2+1]);
}
inline int fdgcd(int x,int l,int r,int a,int b){
	if(l>b||r<a) return INF;
	if(l>=a&&r<=b) return gcd[x];
	if(l==r) return gcd[x];
	int p,q;
	p=fdgcd(x*2,l,(l+r)/2,a,b);
	q=fdgcd(x*2+1,(l+r)/2+1,r,a,b);
	if(p==INF&&q==INF) return INF;
	if(p==INF) return q;
	if(q==INF) return p;
	return __gcd(p,q);
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int i,j,l,r,t,beg,end;
	cin>>n;
	for(i=1;i<=n;i++) cin>>zh[i];
	for(i=1;i<=n;i++) v.push_back(mp(zh[i],i));
	btr(1,1,n);
	sort(v.begin(),v.end());
	cin>>T;
	while(T--){
		cin>>l>>r;
		t=fdgcd(1,1,n,l,r);
		beg=lower_bound(v.begin(),v.end(),mp(t,l))-v.begin();
		end=upper_bound(v.begin(),v.end(),mp(t,r))-v.begin();
		cout<<r-l+1-(end-beg)<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
using namespace std;

#define F first
#define S second

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
//#define int long long

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef pair <ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <ll> vll;
typedef double ld;

const int INF = (int)2e9;
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 10;
const ll LLINF = (ll)1e18 + 10;
const ld pi = acos(-1.0);

template <typename T1, typename T2> bool umin(T1 &x, T2 y){if(x>y) return x=y, true;return false;}
template <typename T1, typename T2> bool umax(T1 &x, T2 y){if(x<y) return x=y, true;return false;}

template <typename T>
T getint() {
    char c = getchar();
    T p = 1, x = 0;
    while(c == ' ' || c == '\n') c = getchar();
    if(c == '-') p = -1, c = getchar();
    while(c >= '0' && c <= '9')
        x = x * 10 + c - '0', c = getchar();
    return x * p;
}

int n;
vpii v;
int le[N], ri[N], a[N];

vpii clean(vpii a) {
    vpii ret;
    for(int i = 0; i < a.size(); ++i)
        if(!i || a[i].F != a[i-1].F)
            ret.pb(a[i]);
    return ret;
}

void add(int val, int id) {
    for(int i = 0; i < v.size(); ++i)
        v[i].F = __gcd(v[i].F, val);
    v.pb(mp(val, id));
    v = clean(v);
}

void get_left(int val, int id) {
    for(int i = 0; i < v.size(); ++i) {
        if(v[i].F%val == 0)
            umin(le[id], v[i].S);
    }
}

void get_right(int val, int id) {
    for(int i = 0; i < v.size(); ++i) {
        if(v[i].F%val == 0)
            umax(ri[id], v[i].S);
    }
}

int qs, sq, b[N];

void init() {
    v.clear();
   // sq = (int)sqrt(n);
    sq = min(n, 250);
    qs = (n + sq - 1) / sq;
    for(int i = 1; i <= n; ++i) {
        le[i] = INF, ri[i] = 0;
        b[i] = b[i-1]; if(b[i]*sq < i) b[i]++;
    }
}

void update(vpii &q, vi &p, int l, int r) {
    int a = --lower_bound(all(q), mp(r, INF)) - q.begin();
    int b = --lower_bound(all(q), mp(l, 0)) - q.begin();
    if(a >= 0) ++p[a];
    if(b >= 0) --p[b];
}

vpii q[N], qb[N];
vi p[N], pb[N];
int ans[N];

int main() {
    //ios_base::sync_with_stdio(0);
 //   freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
    n = getint<int>(); init();
    for(int i = 1; i <= n; ++i) {
        a[i] = getint<int>();
        add(a[i], i);
        get_left(a[i], i);
    }
    bool sam=1;
    for(int i=2;i<=n;i++) if(a[i]!=a[i-1]) sam=0;
    if(sam){
    	int t;
    	scanf("%d",&t);
    	for(int i=1;i<=t;i++) printf("%d\n",0);
    	return 0;
	}
    v.clear();
    for(int i = n; i >= 1; --i) {
        add(a[i], i);
        get_right(a[i], i);
    }
    ////////////////////////////////////////////////////////
    int t = getint<int>();
    for(int i = 0; i < t; ++i) {
        int l = getint<int>(), r = getint<int>();
        q[l].pb(mp(r, i)); qb[b[l]].pb(mp(r, i));
        ans[i] = r - l + 1;
    }
    for(int i = 1; i <= n; ++i) {
        sort(all(q[i]));
        p[i].resize(q[i].size());
    }
    for(int i = 1; i <= qs; ++i) {
        sort(all(qb[i]));
        pb[i].resize(qb[i].size());
    }
    for(int i = 1; i <= n; ++i) {
        int l = le[i], r = ri[i];
      //  cout << l << ' ' << r << '\n';
        for(int j = b[l]; j <= b[i]; ++j) {
            if(j == b[l] || j == b[i]) {
                for(int k = max(sq*j-sq+1, l); k <= min(i, sq*j); ++k) {
                    update(q[k], p[k], i, r);
                }
            } else
            update(qb[j], pb[j], i, r);
        }
    }
    for(int i = 1; i <= n; ++i) {
        int sum = 0;
        for(int j = p[i].size()-1; j >= 0; --j) {
            sum += p[i][j];
            ans[q[i][j].S] -= sum;
        }
    }
    for(int i = 1; i <= qs; ++i) {
        int sum = 0;
        for(int j = pb[i].size()-1; j >= 0; --j) {
            sum += pb[i][j];
            ans[qb[i][j].S] -= sum;
        }
    }
    for(int i = 0; i < t; ++i)
        printf("%d\n", ans[i]);
    return 0;
}


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

#define F first
#define S second

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
//#define int long long

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef pair <ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <ll> vll;
typedef double ld;

const int INF = (int)2e9;
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 10;
const ll LLINF = (ll)1e18 + 10;
const ld pi = acos(-1.0);

template <typename T1, typename T2> bool umin(T1 &x, T2 y){if(x>y) return x=y, true;return false;}
template <typename T1, typename T2> bool umax(T1 &x, T2 y){if(x<y) return x=y, true;return false;}

template <typename T>
T getint() {
    char c = getchar();
    T p = 1, x = 0;
    while(c == ' ' || c == '\n') c = getchar();
    if(c == '-') p = -1, c = getchar();
    while(c >= '0' && c <= '9')
        x = x * 10 + c - '0', c = getchar();
    return x * p;
}

int n;
vpii v;
int le[N], ri[N], a[N];

vpii clean(vpii a) {
    vpii ret;
    for(int i = 0; i < a.size(); ++i)
        if(!i || a[i].F != a[i-1].F)
            ret.pb(a[i]);
    return ret;
}

void add(int val, int id) {
    for(int i = 0; i < v.size(); ++i)
        v[i].F = __gcd(v[i].F, val);
    v.pb(mp(val, id));
    v = clean(v);
}

void get_left(int val, int id) {
    for(int i = 0; i < v.size(); ++i) {
        if(v[i].F%val == 0)
            umin(le[id], v[i].S);
    }
}

void get_right(int val, int id) {
    for(int i = 0; i < v.size(); ++i) {
        if(v[i].F%val == 0)
            umax(ri[id], v[i].S);
    }
}

int qs, sq, b[N];

void init() {
    v.clear();
   // sq = (int)sqrt(n);
    sq = min(n, 250);
    qs = (n + sq - 1) / sq;
    for(int i = 1; i <= n; ++i) {
        le[i] = INF, ri[i] = 0;
        b[i] = b[i-1]; if(b[i]*sq < i) b[i]++;
    }
}

void update(vpii &q, vi &p, int l, int r) {
    int a = --lower_bound(all(q), mp(r, INF)) - q.begin();
    int b = --lower_bound(all(q), mp(l, 0)) - q.begin();
    if(a >= 0) ++p[a];
    if(b >= 0) --p[b];
}

vpii q[N], qb[N];
vi p[N], pb[N];
int ans[N];

int main() {
    //ios_base::sync_with_stdio(0);
 //   freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
    n = getint<int>(); init();
    for(int i = 1; i <= n; ++i) {
        a[i] = getint<int>();
        add(a[i], i);
        get_left(a[i], i);
    }
    bool sam=1;
    for(int i=2;i<=n;i++) if(a[i]!=a[i-1]) sam=0;
    if(sam){
    	int t;
    	scanf("%d",&t);
    	for(int i=1;i<=t;i++) printf("%d\n",0);
    	return 0;
	}
    v.clear();
    for(int i = n; i >= 1; --i) {
        add(a[i], i);
        get_right(a[i], i);
    }
    ////////////////////////////////////////////////////////
    int t = getint<int>();
    for(int i = 0; i < t; ++i) {
        int l = getint<int>(), r = getint<int>();
        q[l].pb(mp(r, i)); qb[b[l]].pb(mp(r, i));
        ans[i] = r - l + 1;
    }
    for(int i = 1; i <= n; ++i) {
        sort(all(q[i]));
        p[i].resize(q[i].size());
    }
    for(int i = 1; i <= qs; ++i) {
        sort(all(qb[i]));
        pb[i].resize(qb[i].size());
    }
    for(int i = 1; i <= n; ++i) {
        int l = le[i], r = ri[i];
      //  cout << l << ' ' << r << '\n';
        for(int j = b[l]; j <= b[i]; ++j) {
            if(j == b[l] || j == b[i]) {
                for(int k = max(sq*j-sq+1, l); k <= min(i, sq*j); ++k) {
                    update(q[k], p[k], i, r);
                }
            } else
            update(qb[j], pb[j], i, r);
        }
    }
    for(int i = 1; i <= n; ++i) {
        int sum = 0;
        for(int j = p[i].size()-1; j >= 0; --j) {
            sum += p[i][j];
            ans[q[i][j].S] -= sum;
        }
    }
    for(int i = 1; i <= qs; ++i) {
        int sum = 0;
        for(int j = pb[i].size()-1; j >= 0; --j) {
            sum += pb[i][j];
            ans[qb[i][j].S] -= sum;
        }
    }
    for(int i = 0; i < t; ++i)
        printf("%d\n", ans[i]);
    return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
//use readint!
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include<map>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stdio.h>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<string.h>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
inline int pt(int a[],int l,int r){
	int p,i,j;
	p=a[l];
	i=l;
	j=r+1;
	for(;;){
		while(a[++i]<p) if(i>=r) break;
		while(a[--j]>p) if(j<=l) break;
		if(i>=j) break;
		else swap(a[i],a[j]);
	}
	if(j==l) return j;
	swap(a[l],a[j]);
	return j;
}
inline void q_sort(int a[],int l,int r){
	int q;
	if(r>l){
		q=pt(a,l,r);
		q_sort(a,l,q-1);
		q_sort(a,q+1,r);
	}
}
inline void rd(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline void quickSort(int s[],int l,int r)  {
    if(l<r){
        int i=l,j=r,x=s[l];
        while(i<j){
            while(i<j&&s[j]>=x) j--;
            if(i<j) s[i++]=s[j];
            while(i<j&&s[i]<x) i++;
            if(i<j) s[j--] = s[i];
        }
        s[i]=x;
        quickSort(s, l, i - 1);
        quickSort(s, i + 1, r);
    }
}
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define FOR(i, a, b) for (int i(a), _b(b); i < _b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, b) for (int i(a), _b(b); i >= _b; --i)
#define FORE(e, v) for (int e = head[v]; e >= 0; e = E[e].next)
#define UN(a) sort(all(a)), (a).erase(unique(all(a)), (a).end())
#define CL(a, v) memset(a, v, sizeof a)
#define pb push_back
#define X first
#define Y second

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll INF_LL = 1000000000000000000LL;
const double pi = 2 * acos(0.0);

template<class T> void smin(T& a, T b) { if (a > b) a = b; }
template<class T> void smax(T& a, T b) { if (a < b) a = b; }
template<class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template<class T> T sqr(T a) { return a * a; }

template<typename Iterator>
inline void print(const char* fmt, Iterator begin, Iterator end,
    const char* separator = " ") {
  for (Iterator it = begin; it != end; ++it) {
    if (it != begin) printf(separator);
    printf(fmt, *it);
  }
  printf("\n");
}

template<int N, class T>
struct TreeOp {
  T (*op)(T, T);
  int n;
  T t[2*N];

  TreeOp(T (*op)(T, T), int n = 0) : op(op), n(n) {}

  T& operator [](int p) { return t[n+p]; }

  inline void build() {
    FORD(i, n-1, 1) t[i] = op(t[i<<1], t[i<<1|1]);
  }

  inline void upd(int p, T newv) {
    for (t[p += n] = newv; p > 1; p >>= 1) t[p>>1] = op(t[p], t[p^1]);
  }

  inline T get(int l, int r) {
    T res = t[l += n];
    for (++l, r += n; l <= r; l = (l+1) >> 1, r = (r-1) >> 1) {
      if ( l&1) res = op(res, t[l]);
      if (~r&1) res = op(res, t[r]);
    }
    return res;
  }
};

inline pii mincnt(pii a, pii b) {
  if (a.X < b.X) return a;
  if (b.X < a.X) return b;
  return pii(a.X, a.Y + b.Y);
}

const int h = 100011;
TreeOp<h, int> tgcd(gcd<int>);
TreeOp<h, pii> tmin(mincnt);
int n;

int main() {
#ifdef LocalHost
  freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
#endif

  scanf("%d", &n);
  tgcd.n = tmin.n = n;
  REP(i, n) {
    scanf("%d", &tgcd[i]);
    tmin[i] = pii(tgcd[i], 1);
  }
  tgcd.build();
  tmin.build();

  int t, l, r;
  scanf("%d", &t);
  REP(i, t) {
    scanf("%d%d", &l, &r);
    --l; --r;
    int ans = r - l + 1;
    pii m = tmin.get(l, r);
    if (m.X == tgcd.get(l, r)) ans -= m.Y;
    printf("%d\n", ans);
  }

#ifdef LocalHost
  cout << endl << endl << "TIME: " << clock() << endl;
#endif
  return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
//use readint!
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include<map>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stdio.h>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<string.h>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int INF=1e9;
inline int pt(int a[],int l,int r){
	int p,i,j;
	p=a[l];
	i=l;
	j=r+1;
	for(;;){
		while(a[++i]<p) if(i>=r) break;
		while(a[--j]>p) if(j<=l) break;
		if(i>=j) break;
		else swap(a[i],a[j]);
	}
	if(j==l) return j;
	swap(a[l],a[j]);
	return j;
}
inline void q_sort(int a[],int l,int r){
	int q;
	if(r>l){
		q=pt(a,l,r);
		q_sort(a,l,q-1);
		q_sort(a,q+1,r);
	}
}
inline void rd(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline void quickSort(int s[],int l,int r)  {
    if(l<r){
        int i=l,j=r,x=s[l];
        while(i<j){
            while(i<j&&s[j]>=x) j--;
            if(i<j) s[i++]=s[j];
            while(i<j&&s[i]<x) i++;
            if(i<j) s[j--] = s[i];
        }
        s[i]=x;
        quickSort(s, l, i - 1);
        quickSort(s, i + 1, r);
    }
}
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define FOR(i, a, b) for (int i(a), _b(b); i < _b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, b) for (int i(a), _b(b); i >= _b; --i)
#define FORE(e, v) for (int e = head[v]; e >= 0; e = E[e].next)
#define UN(a) sort(all(a)), (a).erase(unique(all(a)), (a).end())
#define CL(a, v) memset(a, v, sizeof a)
#define pb push_back
#define X first
#define Y second

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll INF_LL = 1000000000000000000LL;
const double pi = 2 * acos(0.0);

template<class T> void smin(T& a, T b) { if (a > b) a = b; }
template<class T> void smax(T& a, T b) { if (a < b) a = b; }
template<class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template<class T> T sqr(T a) { return a * a; }

template<typename Iterator>
void print(const char* fmt, Iterator begin, Iterator end,
    const char* separator = " ") {
  for (Iterator it = begin; it != end; ++it) {
    if (it != begin) printf(separator);
    printf(fmt, *it);
  }
  printf("\n");
}

template<int N, class T>
struct TreeOp {
  T (*op)(T, T);
  int n;
  T t[2*N];

  TreeOp(T (*op)(T, T), int n = 0) : op(op), n(n) {}

  T& operator [](int p) { return t[n+p]; }

  void build() {
    FORD(i, n-1, 1) t[i] = op(t[i<<1], t[i<<1|1]);
  }

  void upd(int p, T newv) {
    for (t[p += n] = newv; p > 1; p >>= 1) t[p>>1] = op(t[p], t[p^1]);
  }

  T get(int l, int r) {
    T res = t[l += n];
    for (++l, r += n; l <= r; l = (l+1) >> 1, r = (r-1) >> 1) {
      if ( l&1) res = op(res, t[l]);
      if (~r&1) res = op(res, t[r]);
    }
    return res;
  }
};

pii mincnt(pii a, pii b) {
  if (a.X < b.X) return a;
  if (b.X < a.X) return b;
  return pii(a.X, a.Y + b.Y);
}

const int h = 100011;
TreeOp<h, int> tgcd(gcd<int>);
TreeOp<h, pii> tmin(mincnt);
int n;

int main() {
#ifdef LocalHost
  freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
#endif

  scanf("%d", &n);
  tgcd.n = tmin.n = n;
  REP(i, n) {
    scanf("%d", &tgcd[i]);
    tmin[i] = pii(tgcd[i], 1);
  }
  tgcd.build();
  tmin.build();

  int t, l, r;
  scanf("%d", &t);
  REP(i, t) {
    scanf("%d%d", &l, &r);
    --l; --r;
    int ans = r - l + 1;
    pii m = tmin.get(l, r);
    if (m.X == tgcd.get(l, r)) ans -= m.Y;
    printf("%d\n", ans);
  }

#ifdef LocalHost
  cout << endl << endl << "TIME: " << clock() << endl;
#endif
  return 0;
}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>a[i];
    stack<ll >s;
    ll d[n]={0};
    memset(d,-1,sizeof(d));

    s.push(0);
    rep(i,1,n)
    {
     // trace(s.top());
      if(a[i]<a[s.top()])
        {d[i]=0;
          s.push(i);
        }
        else
        {

          while(sz(s)&&a[s.top()]<a[i])
            {
              if(d[s.top()]!=-1)
              d[i]=d[s.top()]+1;
              s.pop();

            }

          s.push(i);
        }
       // trace(i,d[i]);
    }
    ll ma=0;
    rep(i,0,n)
    ma=max(ma,d[i]+1);
    cout<<ma;
  return 0;
  }

  signed main(){

    ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>n;
    ll ma=0,c=0;
    vi v;
    ll cnt=0;
    rep(i,0,n)
    v.pb(a[i]);
    while(1)
    {
      vi t=v;
      v.clear();
      v.pb(t[0]);
    ll x=t[0];
    rep(i,1,sz(t))
    {
      if(a[i]>a[i-1])
      {
        v.pb(a[i]);
      }
    }
    if(sz(t)==sz(v))
      break;
    cnt++;

    if(sz(v)==1)
      break;
    }
    cout<<cnt;
  return 0;
  }

  signed main(){

    //ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>a[i];
    stack<ll >s;
    ll d[n]={0};
    memset(d,-1,sizeof(d));

    s.push(0);
    rep(i,1,n)
    {
     // trace(s.top());
      if(a[i]<a[s.top()])
        {d[i]=0;
          s.push(i);
        }
        else
        {

          while(sz(s)&&a[s.top()]<a[i])
            {
              if(d[s.top()]!=-1)
              d[i]=max(d[s.top()]+1,d[i]);
              s.pop();

            }

          s.push(i);
        }
       // trace(i,d[i]);
    }
    ll ma=0;
    rep(i,0,n)
    ma=max(ma,d[i]+1);
    cout<<ma;
  return 0;
  }

  signed main(){

    ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>a[i];
    ll ma=0,c=0;
    vi v;
    ll cnt=0;
    rep(i,0,n)
    v.pb(a[i]);
    while(1)
    {
      vi t=v;
     // trace(sz(t),sz(v));
      v.clear();
     // trace(sz(v));

    ll x=t[0];
    ll f=0;
    rep(i,1,sz(t))
    {
      if(t[i]>t[i-1])
      {
        if(x>t[i])
        v.pb(t[i]);
        else
          {x=t[i];
            f=0;
          }
      }
      else
      {
        if(f==0)
        {
          f=1;
          v.pb(x);
        }
      }
    }
    if(sz(t)==sz(v)||sz(v)==0)
      break;
    cnt++;
   // trace(cnt,sz(v));
    if(sz(v)<=1)
      break;
    }
    cout<<cnt;
  return 0;
  }

  signed main(){

    ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>a[i];
    stack<ll >s;
    ll d[n]={0};
    memset(d,-1,sizeof(d));

    s.push(0);
    rep(i,1,n)
    {
     // trace(s.top());
      if(a[i]<a[s.top()])
        {d[i]=0;
          s.push(i);
        }
        else
        {

          while(sz(s)&&a[s.top()]<a[i])
            {
              if(d[s.top()]!=-1)
              d[i]=max(d[s.top()]+1,d[i]);
              s.pop();

          }
          if(sz(s)==0)
            d[i]=-1;
          s.push(i);
        }
       // trace(i,d[i]);
    }
    ll ma=0;
    rep(i,0,n)
    ma=max(ma,d[i]+1);
    cout<<ma;
  return 0;
  }

  signed main(){

    ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>a[i];
    ll ma=0,c=0;
    vi v;
    ll cnt=0;
    rep(i,0,n)
    v.pb(a[i]);
    while(1)
    {
      vi t=v;
     // trace(sz(t),sz(v));
      v.clear();
     // trace(sz(v));
      v.pb(t[0]);
    ll x=t[0];
    rep(i,1,sz(t))
    {
      if(t[i]>t[i-1])
      {
        v.pb(t[i]);
      }
    }
    if(sz(t)==sz(v))
      break;
    cnt++;
   // trace(cnt,sz(v));
    if(sz(v)==1)
      break;
    }
    cout<<cnt;
  return 0;
  }

  signed main(){

    ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


//..............code of a beginner................//

 //#pragma GCC optimize("Ofast")
 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 //#pragma GCC optimize("unroll-loops")
// The simple the code, the better the speedup

  #include<bits/stdc++.h>
  #include <ext/pb_ds/assoc_container.hpp>
  #include <ext/pb_ds/tree_policy.hpp>

  using namespace std;
  using namespace __gnu_pbds;

    struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    };
  //to avoid multiple collisions in the hash table
  // umap m;

  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // use rng() to generate random numbers

  typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    newset;
  //newset s;
  //*s.find_by_order(x)  for value at index x
  //s.order_of_key(x)   for finding the number at x

  #define ll            long long
  #define pb            push_back
  #define ppb           pop_back
  #define si            set <ll>
  #define endl          '\n'
  #define F             first
  #define S             second
  #define mii           map<ll ,ll >
  #define umap          unordered_map<long long, int, custom_hash>
  #define msi           map<string,ll >
  #define rep(i,a,b)    for(ll  i=a;i<b;i++)
  #define all(v)        v.begin(),v.end()
  #define pii           pair<ll ,ll >
  #define vi            vector<ll >
  #define vii           vector<pair<ll , ll > >
  #define sz(x)         (ll )x.size()
  #define sl(x)          (ll )x.length()
  #define hell          1000000007
  #define mp            make_pair
  #define sp(n)         setprecision(n)<<fixed
  #define spl           " "
  #define mod            998244353
  #define ios     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  #define M       10000001

      #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
      template <typename Arg1>
      void __f(const char* name, Arg1&& arg1){
          std::cerr << name << " : " << arg1 << endl;
      }
      template <typename Arg1, typename... Args>
      void __f(const char* names, Arg1&& arg1, Args&&... args){
          const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
      }

/*-------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};          // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};          // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};   // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1};   // Knights Move
/*------------------------------------------------*/

  //#define abc   cin
  //#define qwe   cout

  ll power(ll base,ll exp) {
    ll res=1;
    while(exp>0) {
      if(exp%2==1) res=((res%mod)*(base%mod))%mod;
        base=((base%mod)*(base%mod)%mod);
        exp/=2;}
    return res%mod;}

  //..............Code Begins Here................//
  ll n,k;

  int solve()
  {
    cin>>n;
    ll a[n];
    rep(i,0,n)
    cin>>a[i];
    stack<ll >s;
    ll d[n]={0};
    memset(d,-1,sizeof(d));

    s.push(0);
    rep(i,1,n)
    {
     // trace(s.top());
      if(a[i]<a[s.top()])
        {d[i]=0;
          s.push(i);
        }
        else
        {
          if(d[s.top()]!=-1)
          d[i]=d[s.top()]+1;

          while(sz(s)&&a[s.top()]<a[i])
            s.pop();

          s.push(i);
        }
    }
    ll ma=0;
    rep(i,0,n)
    ma=max(ma,d[i]+1);
    cout<<ma;
  return 0;
  }

  signed main(){

    ios

    int test=1;

   // cin>>test;

    while(test--){
      solve();}

    return 0;}


#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vl = vector<ll>;
using vll = vector<pll>;
using ld = long double;
#define ff first
#define ss second
const ld pi = acosl(-1);
const ll prime = 1000000000 + 7;
const ll INF = 1000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, res = 0;
    cin >> n;
    vl vet(n+2);
    vl maior(n+2);
    vet[0] = 0;
    map<ll, ll> conj;
    priority_queue<pair<pll, ll>, vector<pair<pll, ll>>, greater<pair<pll, ll>>> fila;
    vl foi(n+1);
    for(int i = 1; i <= n; ++i){
        cin >> vet[i];
    }
    vet[n+1] = n+1;
    conj[0] = 0;
    ll aux = 1;
    for(int i = 1; i <= n; ++i){
        conj[i] = 0;
        if(vet[i] < vet[i+1]){
            conj[i] = 1;
            fila.push({{0, -i}, i});
            maior[i] = 0;
        }
    }
    ll z = 0;
    while(!fila.empty() && z++ < 4*n){
        auto a = fila.top();
        fila.pop();
        if(a.ff.ff != maior[a.ss]){
            continue;
        }
        if(foi[a.ss]){
            continue;
        }
        foi[a.ss] = 1;
        auto it = conj.lower_bound(a.ss);
        it--;
        if(vet[it->ff] > vet[a.ss]){
            res = max(res, ++it->ss);
            conj.erase(a.ss);
            fila.push({{a.ff.ff, -it->ff}, it->ff});
            maior[it->ff] = a.ff.ff;
            // foi[it->ff] = 1;
        } else{
            fila.push({{a.ff.ff + 1, -a.ss}, a.ss});
            maior[a.ss] = a.ff.ff + 1;
            foi[a.ss] = 0;
        }
    }
    cout << res << endl;

    return 0;
}

/*
10
10 9 7 8 6 5 3 4 2 1
*/


#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define vl vector<ll>
#define vll vector<pll>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi acosl(-1)
#define ld long double
#define prime (1000000000 + 7)
#define INF 1000000000

using namespace std;

bool check(vl &vet){
	for(int i = 1; i < vet.size(); ++i){
		if(vet[i-1] > vet[i]){
			return true;
		}
	}
	return false;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	ll n;
	cin >> n;
	vl vet(n);
	for(auto&p:vet){
		cin >> p;
	}
	ll res = 0;
	vl aux;
	aux = vet;
	aux.push_back(0x3f3f3f3f);
	while (check(aux)){
		res++;
		vet.clear();
		for(int i = 0; i < aux.size(); ++i){
			vet.push_back(aux[i]);
			for(int j = i + 1; j < aux.size(); ++j){
				if(aux[j-1] > aux[j]){

				} else{
					i = j - 1;
					break;
				}
			}
		}
		aux = vet;
	}

	cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vl = vector<ll>;
using vll = vector<pll>;
using ld = long double;
#define ff first
#define ss second
const ld pi = acosl(-1);
const ll prime = 1000000000 + 7;
const ll INF = 1000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, res = 0;
    cin >> n;
    vl vet(n+2);
    vl maior(n+2);
    vet[0] = 0;
    map<ll, ll> conj;
    priority_queue<pair<pll, ll>, vector<pair<pll, ll>>, greater<pair<pll, ll>>> fila;
    vl foi(n+1);
    for(int i = 1; i <= n; ++i){
        cin >> vet[i];
    }
    vet[n+1] = n+1;
    conj[0] = 0;
    ll aux = 1;
    for(int i = 1; i <= n; ++i){
        conj[i] = 1;
        if(vet[i] < vet[i+1]){
            // conj[i] = 1;
            fila.push({{0, -i}, i});
            maior[i] = 0;
        }
    }
    ll z = 0;
    stack<pll> pilha;
    while(!fila.empty() && z != conj.size() + pilha.size()){
        z = conj.size() + pilha.size();
        auto a = fila.top();
        fila.pop();
        if(a.ff.ff != maior[a.ss]){
            continue;
        }
        if(foi[a.ss]){
            continue;
        }
        foi[a.ss] = 1;
        auto it = conj.lower_bound(a.ss);
        it--;
        if(vet[it->ff] > vet[a.ss]){
            res = max(res, it->ss++);
            conj.erase(a.ss);
            fila.push({{a.ff.ff, -it->ff}, it->ff});
            maior[it->ff] = a.ff.ff;
            // foi[it->ff] = 1;
            ll u = 1;
            while(!pilha.empty()){
                auto b = pilha.top();
                pilha.pop();
                fila.push({{a.ff.ff + u, -b.ss}, b.ss});
                maior[b.ss] = a.ff.ff + u;
                foi[b.ss] = 0;
                u++;
            }
        } else{
            pilha.push({a.ff.ff, a.ss});

            // fila.push({{a.ff.ff + 1, -a.ss}, a.ss});
            // maior[a.ss] = a.ff.ff + 1;
            // foi[a.ss] = 0;
        }
    }
    cout << res << endl;

    return 0;
}

/*
10
10 9 7 8 6 5 3 4 2 1
*/


#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vl = vector<ll>;
using vll = vector<pll>;
using ld = long double;
#define ff first
#define ss second
const ld pi = acosl(-1);
const ll prime = 1000000000 + 7;
const ll INF = 1000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, res = 0;
    cin >> n;
    vl vet(n+2);
    vl maior(n+2);
    vet[0] = 0;
    map<ll, ll> conj;
    priority_queue<pair<pll, ll>, vector<pair<pll, ll>>, greater<pair<pll, ll>>> fila;
    vl foi(n+1);
    for(int i = 1; i <= n; ++i){
        cin >> vet[i];
    }
    vet[n+1] = n+1;
    conj[0] = 0;
    ll aux = 1;
    for(int i = 1; i <= n; ++i){
        conj[i] = 0;
        if(vet[i] < vet[i+1]){
            conj[i] = 1;
            fila.push({{0, aux++}, i});
            maior[i] = 0;
            foi[i] = 1;
        }
    }
    while(!fila.empty()){
        auto a = fila.top();
        fila.pop();
        if(a.ff.ff != maior[a.ss]){
            continue;
        }
        auto it = conj.lower_bound(a.ss);
        it--;
        if(vet[it->ff] > vet[a.ss]){
            res = max(res, ++it->ss);
            conj.erase(a.ss);
            if(foi[it->ff]){
                fila.push({{a.ff.ff + 1, aux++}, it->ff});
                maior[it->ff] = a.ff.ff + 1;
            } else{
                fila.push({{a.ff.ff, aux++}, it->ff});
                maior[it->ff] = a.ff.ff;
            }
            foi[it->ff] = 1;
        }
    }
    cout << res << endl;

    return 0;
}

/*
10
10 9 7 8 6 5 3 4 2 1
*/


#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vl = vector<ll>;
using vll = vector<pll>;
using ld = long double;
#define ff first
#define ss second
const ld pi = acosl(-1);
const ll prime = 1000000000 + 7;
const ll INF = 1000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, res = 0;
    cin >> n;
    stack<pll> pilha1, pilha2;
    for(int i = 0; i < n; ++i){
        ll a;
        cin >> a;
        pilha1.push({a, 0});
    }
    ll size = 0;
    while(pilha1.size() > 0 && pilha1.size() + pilha2.size() > 0 && pilha1.size() + pilha2.size() != size){
        size = pilha1.size() + pilha2.size() ;
        pll aux1 = pilha1.top();
        pilha1.pop();
        while(!pilha1.empty()){
            if(pilha1.top().ff > aux1.ff){
                pilha1.top().ss++;
                res = max(res, pilha1.top().ss);
                aux1 = pilha1.top();
                pilha1.pop();
            } else{
                break;
            }
        }
        pilha1.push(aux1);
        while(!pilha2.empty()){
            if(pilha2.top().ff < pilha1.top().ff){
                pilha1.top().ss = max(pilha1.top().ss + 1, pilha2.top().ss);
                res = max(res, pilha1.top().ss);
                pilha2.pop();
            } else{
                break;
            }
        }
        pilha2.push(pilha1.top());
        pilha1.pop();
    }
    cout << res << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vl = vector<ll>;
using vll = vector<pll>;
using ld = long double;
#define ff first
#define ss second
const ld pi = acosl(-1);
const ll prime = 1000000000 + 7;
const ll INF = 1000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, res = 0;
    cin >> n;
    vl vet(n+2);
    vl maior(n+2);
    vet[0] = 0;
    map<ll, ll> conj;
    priority_queue<pair<pll, ll>, vector<pair<pll, ll>>, greater<pair<pll, ll>>> fila;
    vl foi(n+1);
    for(int i = 1; i <= n; ++i){
        cin >> vet[i];
    }
    vet[n+1] = n+1;
    conj[0] = 0;
    ll aux = 1;
    for(int i = 1; i <= n; ++i){
        conj[i] = 1;
        if(vet[i] < vet[i+1]){
            // conj[i] = 1;
            fila.push({{0, -i}, i});
            maior[i] = 0;
        }
    }
    ll z = 0;
    stack<pll> pilha;
    while(!fila.empty() && z++ < 10000000){
        auto a = fila.top();
        fila.pop();
        if(a.ff.ff != maior[a.ss]){
            continue;
        }
        if(foi[a.ss]){
            continue;
        }
        foi[a.ss] = 1;
        auto it = conj.lower_bound(a.ss);
        it--;
        if(vet[it->ff] > vet[a.ss]){
            res = max(res, it->ss++);
            conj.erase(a.ss);
            fila.push({{a.ff.ff, -it->ff}, it->ff});
            maior[it->ff] = a.ff.ff;
            // foi[it->ff] = 1;
            ll u = 1;
            while(!pilha.empty()){
                auto b = pilha.top();
                pilha.pop();
                fila.push({{a.ff.ff + u, -b.ss}, b.ss});
                maior[b.ss] = a.ff.ff + u;
                foi[b.ss] = 0;
                u++;
            }
        } else{
            pilha.push({a.ff.ff, a.ss});

            // fila.push({{a.ff.ff + 1, -a.ss}, a.ss});
            // maior[a.ss] = a.ff.ff + 1;
            // foi[a.ss] = 0;
        }
    }
    cout << res << endl;

    return 0;
}

/*
10
10 9 7 8 6 5 3 4 2 1
*/


#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
	#define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
#else
	#define W(x, y)
#endif

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vl = vector<ll>;
using vll = vector<pll>;
using ld = long double;
#define ff first
#define ss second
const ld pi = acosl(-1);
const ll prime = 1000000000 + 7;
const ll INF = 1000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, res = 0;
    cin >> n;
    vl vet(n+2);
    vl maior(n+2);
    vet[0] = 0;
    map<ll, ll> conj;
    priority_queue<pair<pll, ll>, vector<pair<pll, ll>>, greater<pair<pll, ll>>> fila;
    vl foi(n+1);
    for(int i = 1; i <= n; ++i){
        cin >> vet[i];
    }
    vet[n+1] = n+1;
    conj[0] = 0;
    ll aux = 1;
    for(int i = 1; i <= n; ++i){
        conj[i] = 1;
        if(vet[i] < vet[i+1]){
            // conj[i] = 1;
            fila.push({{0, -i}, i});
            maior[i] = 0;
        }
    }
    ll z = 0;
    stack<pll> pilha;
    while(!fila.empty() && z++ < 4*n){
        auto a = fila.top();
        fila.pop();
        if(a.ff.ff != maior[a.ss]){
            continue;
        }
        if(foi[a.ss]){
            continue;
        }
        foi[a.ss] = 1;
        auto it = conj.lower_bound(a.ss);
        it--;
        if(vet[it->ff] > vet[a.ss]){
            res = max(res, it->ss++);
            conj.erase(a.ss);
            fila.push({{a.ff.ff, -it->ff}, it->ff});
            maior[it->ff] = a.ff.ff;
            // foi[it->ff] = 1;
            ll u = 1;
            while(!pilha.empty()){
                auto b = pilha.top();
                pilha.pop();
                fila.push({{a.ff.ff + u, -b.ss}, b.ss});
                maior[b.ss] = a.ff.ff + u;
                foi[b.ss] = 0;
                u++;
            }
        } else{
            pilha.push({a.ff.ff, a.ss});

            // fila.push({{a.ff.ff + 1, -a.ss}, a.ss});
            // maior[a.ss] = a.ff.ff + 1;
            // foi[a.ss] = 0;
        }
    }
    cout << res << endl;

    return 0;
}

/*
10
10 9 7 8 6 5 3 4 2 1
*/


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll (i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second
int main()
{
    lol;
    ll n;cin >> n;
    vector <ll> v(n,0);
    for(ll i=0;i<n;i++) cin >> v[i];
    ll ans = 0;
    while(1)
    {
        // for(ll i=0;i<v.size();i++)
        // {
        //     cout << v[i] << " ";
        // }
        // cout << endl;
        if(v.size()<=1) break;
        vector <ll> v1;
        v1.pb(v[0]);
        for(ll i=1;i<v.size();i++)
        {
            if(v[i]<v[i-1]) continue;
            else
            {
                v1.pb(v[i]);
            }
        }
        if(v1.size()==v.size()) break;
        ans++;
        for(ll i=0;i<v1.size();i++)
        {
            v[i] = v1[i];
        }
        v.resize(v1.size());
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll (i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second
int main()
{
    lol;
    int n;cin >> n;
    stack <ll> s;int t[n+1];
    for(int i=0;i<=n;i++) t[i] = -1;
    int f1;cin >> f1;
    s.push(f1);
    t[f1] = -1;int ans = 0;
    for(int i=1;i<n;i++)
    {
        int ele;cin >> ele;
        t[ele] = 0;
        while(s.size()>0 && ele>s.top())
        {
            t[ele] = max(t[s.top()]+1,t[ele]);
            s.pop();
        }
        if(s.size()==0)
        {
            t[ele] = -1;
        }
        s.push(ele);
        ans = max(ans,t[ele]+1);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll (i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second
int main()
{
    lol;
    int n;cin >> n;
    stack <ll> s;int t[n+1];
    for(int i=0;i<=n;i++) t[i] = -1;
    int f1;cin >> f1;
    s.push(f1);
    t[f1] = -1;int ans = 0;
    for(int i=1;i<n;i++)
    {
        int ele;cin >> ele;
        t[ele] = 0;
        while(s.size()>0 && ele>s.top())
        {
            t[ele] = max(t[ele],t[s.top()]+1);
            s.pop();
        }
        if(s.size()==0)
        {
            t[ele] = -1;
        }
        s.push(ele);
        ans = max(ans,t[ele]+1);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll (i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second
int main()
{
    lol;
    int n;cin >> n;
    stack <ll> s;int t[n+1];
    for(int i=0;i<=n;i++) t[i] = -1;
    int f1;cin >> f1;
    s.push(f1);
    t[f1] = -1;int ans = 0;
    for(int i=1;i<n;i++)
    {
        int ele;cin >> ele;
        t[ele] = 0;
        while(s.size()>0 && ele>s.top())
        {
            t[ele] = t[s.top()]+1;
            s.pop();
        }
        if(s.size()==0)
        {
            t[ele] = -1;
        }
        s.push(ele);
        ans = max(ans,t[ele]+1);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll (i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second
int main()
{
    lol;
    ll n;cin >> n;
    vector <ll> v(n,0);
    for(ll i=0;i<n;i++) cin >> v[i];
    ll ans = 0;
    while(1)
    {
        if(v.size()<=1) break;
        vector <ll> v1;
        ll cnt = 0;
        v1.pb(v[0]);
        for(ll i=1;i<v.size();i++)
        {
            if(v[i]<v[i-1]) {cnt++;continue;}
            else
            {
                if(cnt==0) v1.pop_back();
                cnt = 0;
                v1.pb(v[i]);
            }
        }
        if(cnt==0 && v1.size()>=1) v1.pop_back();
        for(ll i=0;i<v1.size();i++)
        {
            v[i] = v1[i];
        }
        v.resize(v1.size());
        if(v1.size()==0) break;
        ans++;
    }
    cout << ans << endl;
}


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

const int N = 1e5 + 10, INF = 1e8 + 119;
int a, n, ans, l, b[N];
deque<pair<int, int> > stac;
vector<int> times;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a;
		l = 0;
		while((int)stac.size() > 0 && stac.back().first < a){
		//	cout << stac.back().first << endl;
			l = max(stac.back().second, l);
			stac.pop_back();
		}
		if((int)stac.size() == 0 || (l == INF))
			stac.push_back({a, INF});
		else
			stac.push_back({a, l + 1}), times.push_back(l + 1);
	}
	//cout << "OUT" << endl;
	for (int i : time)
		ans = max(ans, i);
	cout << ans;

	return 0;
}


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

const int N = 1e5 + 10, INF = 1e8 + 119;
int a, n, ans, l, b[N];
deque<pair<int, int> > stac;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a;
		l = 0;
		while(stac.size() > 0 && stac.back().first < a){
		//	cout << stac.back().first << endl;
			l = max(stac.back().second, l);
			stac.pop_back();
		}
		if(stac.size() == 0 && i != 0)
			l = N - 1;
		stac.push_back({a, l + 1});
	}
	//cout << "OUT" << endl;
	for (auto i : stac)
		if(i.second != N)
			ans = max(ans, i.second);
	cout << ans;

	return 0;
}


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

const int N = 1e5 + 10, INF = 1e8 + 119;
int a, n, ans, l, b[N];
deque<pair<int, int> > stac;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a;
		l = 0;
		while(stac.size() > 0 && stac.back().first < a){
		//	cout << stac.back().first << endl;
			l = max(stac.back().second, l);
			stac.pop_back();
		}
		if((stac.size() == 0 && i != 0) || (l == N))
			stac.push_back({a, N});
		else
			stac.push_back({a, l + 1});
	}
	//cout << "OUT" << endl;
	for (auto i : stac)
		if(i.second != N)
			ans = max(ans, i.second);
	cout << ans;

	return 0;
}


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

const int N = 1e5 + 10, INF = 1e8 + 119;
int a, n, ans, l, b[N];
deque<pair<int, int> > stac;
vector<int> times;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a;
		l = 0;
		while((int)stac.size() > 0 && stac.back().first < a){
		//	cout << stac.back().first << endl;
			l = max(stac.back().second, l);
			stac.pop_back();
		}
		if((int)stac.size() == 0 || (l == INF))
			stac.push_back({a, INF});
		else
			stac.push_back({a, l + 1}), times.push_back(l + 1);
	}
	//cout << "OUT" << endl;
	for (int i : times)
		ans = max(ans, i);
	cout << ans;

	return 0;
}


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

const int N = 1e5 + 10, INF = 1e8 + 119;
int a, n, ans, l, b[N];
deque<pair<int, int> > stac;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a;
		l = 0;
		while(stac.size() > 0 && stac.back().first < a){
		//	cout << stac.back().first << endl;
			l = max(stac.back().second, l);
			stac.pop_back();
		}
		if(stac.size() == 0)
			l = N - 1;
		stac.push_back({a, l + 1});
	}
	//cout << "OUT" << endl;
	for (auto i : stac)
		if(i.second != N)
			ans = max(ans, i.second);
	cout << ans;

	return 0;
}


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

int n;
vector<int> vi;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		vi.push_back(x);
	}
	int ans = 0;
	bool ubah = 1;
	while(ubah){
		ans++;
		ubah = 0;
		vector<int> tmp;
		tmp.push_back(vi[0]);
		for(int i=1;i<vi.size();i++){
			if(vi[i] < vi[i-1]){
				ubah = 1;
			} else {
				tmp.push_back(vi[i]);
			}
		}
		vi = tmp;
	}
	printf("%d\n",ans-1);
	return 0;
}


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

typedef pair<int,int> pii;
const int N = 1e5;

int n;
int li[N+5];
vector<int> vi;

int main(){
	scanf("%d",&n);
	bool kena = 0;
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		li[i] = x;
		if(i>1 && li[i] < li[i-1]){
			kena = 1;
		}
		if(i>2 && li[i] < li[i-1] && li[i-1] < li[i-2]){
//			cout << " THROW " << vi.back() << endl;
			vi.pop_back();
		}
		vi.push_back(x);
	}
	if(!kena){
		printf("0\n");
		return 0;
	}
//	for(int i=0;i<vi.size();i++){
//		printf("%d ",vi[i]);
//	}
//	printf("\n");
	int ans = 1;
	stack<int> st;
	for(int i=vi.size()-1;i>=0;i--){
		int tmp = 0;
		while(!st.empty() && st.top() < vi[i]){
			tmp++;
			st.pop();
		}
		st.push(vi[i]);
		ans = max(ans,tmp);
	}
	printf("%d\n",ans);
	return 0;
}

/*
10
10 7 4 2 5 8 9 6 3 1
*/


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

const int N = 1e5;

int n;
int li[N+5];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		li[i] = x;
	}
	int ans = 0;
	stack<int> st;
	for(int i=n;i>=1;i--){
		int tmp = 0;
		while(!st.empty() && li[i] > st.top()){
			st.pop();
			tmp++;
		}
		ans = max(ans,tmp);
		st.push(li[i]);
	}
	printf("%d\n",ans);
	return 0;
}


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

typedef pair<int,int> pii;
const int N = 1e5;

//int find_par(int x, int *par){
//	if(par[x] == x) return x;
//	retun
//}

int n;
int li[N+5];
int par[N+5];
queue<int> vi;

int main(){
	scanf("%d",&n);
	bool kena = 0;
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		li[i] = x;
		par[i] = i;
//		if(i>2 && li[i] < li[i-1] && li[i-1] < li[i-2]){
////			cout << " THROW " << vi.back() << endl;
//			vi.pop_back();
//		}
//		vi.push_back(x);
	}
	par[n+1] = n+1;
	for(int i=n;i>=2;i--){
		if(i>1 && li[i] < li[i-1]){
			vi.push(i-1);
		}
	}
//	for(int i=0;i<vi.size();i++){
//		printf("%d ",vi[i]);
//	}
//	printf("\n");
	int ans = 0;
//	stack<int> st;
//	for(int i=vi.size()-1;i>=0;i--){
//		int tmp = 0;
//		while(!st.empty() && st.top() < vi[i]){
//			tmp++;
//			st.pop();
//		}
//		st.push(vi[i]);
//		ans = max(ans,tmp);
//	}
	while(!vi.empty()){
		bool tam = 0;
		int pan = vi.size();
		for(int i=0;i<pan;i++){
			int cur = vi.front(); vi.pop();
			if(par[cur]!=cur) continue;
//			cout << "NOW PROC " << cur << endl;
			int seb = par[cur+1];
			if(seb<=n && li[cur] > li[seb]){
//				cout << "DESTROY " << seb << endl;
				tam=1;
				par[cur+1] = par[seb+1];
				vi.push(cur);
			}
		}
		if(tam) ans++;
	}
	printf("%d\n",ans);
	return 0;
}

/*
10
10 7 4 2 5 8 9 6 3 1
*/


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

typedef pair<int,int> pii;
const int N = 1e5;

int n;
int li[N+5];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		li[i] = x;
	}
	int ans = 0;
	stack<pii> st;
	for(int i=n;i>=1;i--){
		int tmp = 0;
		while(!st.empty() && li[i] > st.top().first){
			tmp+=max(1,st.top().second);
			st.pop();
		}
		ans = max(ans,tmp);
		st.push(make_pair(li[i],tmp));
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define loop(i, a, b) for(lli i=a; i<b; i++)
#define loopb(i, a, b) for(lli i=a; i>=b; i--)
#define pb push_back
#define umap unordered_map
typedef long long int lli;
typedef pair<lli, lli> pii;
typedef vector<lli> vll;
typedef tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// use to avoid collision
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
// for pair in unordered_map
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
lli power(lli x, lli y){
    lli res = 1;
    x = x ;
    while (y > 0) {
        if (y & 1)
            res = (res*x) ;
        y = y>>1;
        x = (x*x);
    }
    return res;
}
lli M = 1e9+7;
bool same(vll& A, vll& B){
	lli a = A.size();
	lli b = B.size();
	if(a != b)
		return false;
	else
		return true;
}
lli solve(){
	lli n;
	cin>>n;
	vll S(n);
	loop(i, 0, n)	cin>>S[i];
	lli ans = 0;
	while(true){
		lli i=0, j=0;
		vll temp;
		temp.pb(S[0]);
		lli sz = S.size();
		while(j < sz){
			if(j < sz-1 && S[j] < S[j+1]){
				temp.pb(S[j+1]);
			}
			j++;
		}
		if(temp.size() == S.size())
			break;
		ans++;
		S.clear();
		S = temp;
	}
//	while(true){
//		lli i = 0;
//		lli j = 0;
//		vll temp;
//		lli sz = S.size();
//		while(i < sz && j < sz){
//			if(A[j] > A[j+1])
//				j++;
//			else{
//				temp.pb(A[i]);
//				j++;
//				i = j;
//			}
//		}
//		if(same(temp, S))
//			break;
//		S.clear();
//		S = temp;
//		ans++;
//	}
	cout<<ans<<"\n";
	return 0;
}
int main(){
	fastio;
	lli t;
//	cin>>t;
	t = 1;
	while(t>0){
		t--;
		solve();
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define loop(i, a, b) for(lli i=a; i<b; i++)
#define loopb(i, a, b) for(lli i=a; i>=b; i--)
#define pb push_back
#define umap unordered_map
typedef long long int lli;
typedef pair<lli, lli> pii;
typedef vector<lli> vll;
typedef tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// use to avoid collision
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
// for pair in unordered_map
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
lli power(lli x, lli y){
    lli res = 1;
    x = x ;
    while (y > 0) {
        if (y & 1)
            res = (res*x) ;
        y = y>>1;
        x = (x*x);
    }
    return res;
}
lli M = 1e9+7;
bool same(vll& A, vll& B){
	lli a = A.size();
	lli b = B.size();
	if(a != b)
		return false;
	else
		return true;
}
lli solve(){
	lli n;
	cin>>n;
	vll A(n);
	loop(i, 0, n)	cin>>A[i];
	lli ans = 0;
	vll S = A;
	while(true){
		lli i = 0;
		lli j = 0;
		vll temp;
		lli sz = S.size();
		while(i < sz && j < sz){
			if(A[j] > A[j+1])
				j++;
			else{
				temp.pb(A[i]);
				j++;
				i = j;
			}
		}
		if(same(temp, S))
			break;
		S.clear();
		S = temp;
		ans++;
	}
	cout<<ans<<"\n";
	return 0;
}
int main(){
	fastio;
	lli t;
//	cin>>t;
	t = 1;
	while(t>0){
		t--;
		solve();
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define loop(i, a, b) for(lli i=a; i<b; i++)
#define loopb(i, a, b) for(lli i=a; i>=b; i--)
#define pb push_back
#define umap unordered_map
typedef long long int lli;
typedef pair<lli, lli> pii;
typedef vector<lli> vll;
typedef tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// use to avoid collision
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
// for pair in unordered_map
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
lli power(lli x, lli y){
    lli res = 1;
    x = x ;
    while (y > 0) {
        if (y & 1)
            res = (res*x) ;
        y = y>>1;
        x = (x*x);
    }
    return res;
}
lli M = 1e9+7;
bool same(vll& A, vll& B){
	lli a = A.size();
	lli b = B.size();
	if(a != b)
		return false;
	else
		return true;
}
lli solve(){
	lli n;
	cin>>n;
	vll S(n);
	loop(i, 0, n)	cin>>S[i];
	vll death_time(n, 0);
	stack<lli> st;
	st.push(0);
	death_time[0] = -1;
	lli time = 0;
	loop(i, 1, n){
		while(!st.empty() && S[st.top()] < S[i]){
			death_time[i] = max(death_time[i], death_time[st.top()]+1);
			st.pop();
		}
		if(st.size() == 0){
//			st.push(i);
			death_time[i] = -1;
		}
		st.push(i);
		time = max(time, death_time[i]+1);
	}
	cout<<time<<"\n";
	return 0;
}
int main(){
	fastio;
	lli t;
//	cin>>t;
	t = 1;
	while(t>0){
		t--;
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0)
#define loop(i, a, b) for(lli i=a; i<b; i++)
#define loopb(i, a, b) for(lli i=a; i>=b; i--)
#define pb push_back
#define umap unordered_map
typedef long long int lli;
typedef pair<lli, lli> pii;
typedef vector<lli> vll;
// lli M = 1e9+7;
bool same(vll& A, vll& B){
	lli a = A.size();
	lli b = B.size();
	if(a != b)
		return false;
	else
		return true;
}
lli solve(){
	lli n;
	cin>>n;
	vll S(n);
	loop(i, 0, n)	cin>>S[i];
	lli ans = 0;
	while(true){
		lli i = 0;
		lli j = 0;
		vll temp;
		lli sz = S.size();
		while(i < sz && j < sz){
			if(j < sz-1 && S[j] > S[j+1])
				j++;
			else{
				temp.pb(S[i]);
				j++;
				i = j;
			}
		}
		if(same(temp, S))
			break;
		S.clear();
		S = temp;
		ans++;
	}
	cout<<ans<<"\n";
	return 0;
}
int main(){
	fastio;
	lli t;
//	cin>>t;
	t = 1;
	while(t>0){
		t--;
		solve();
	}
}