#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
#define eps 1e-9

using namespace std;

typedef long long ll;

int n, v[100002], ans[100002], ansmx;

int rungg()
{
    ansmx = 0;
    set<int> s;
    set<int> lft;
    for(int i = 1; i <= n; ++i)
        if(v[i] < v[i-1])
            s.insert(i);
        else
            lft.insert(i);
    while(!s.empty())
    {
        set<int> s2;
        ++ansmx;
        while(!s.empty())
        {
            int val = *s.begin();
            s.erase(val);
            int nxt = n+1;
           // cout << ansmx << " " << val << '\n';
            set<int> ::iterator it = lft.lower_bound(val);
            if(it != lft.end())
            {
                nxt = *lft.lower_bound(val);
              //  cout << nxt << " " << *lft.begin() << '\n';
                if(it != lft.begin())
                {
                    --it;
                 //   cout << *it << " " << nxt << " " << val << " " << v[*it] << " " << v[nxt] << '\n';
                    if(v[*it] > v[nxt])
                        s2.insert(nxt);
                }
            }
        }
        s = s2;
        while(!s2.empty())
        {
            lft.erase(*s2.rbegin());
            s2.erase(*s2.rbegin());
        }
    }
    return ansmx;
}
int runbrut()
{
    ansmx = 0;
    bool ok = 1;
    vector<int> vx;
    for(int i = 1; i <= n; ++i)
        vx.pb(v[i]);
    vector<int> v2;
    while(ok)
    {
        ok = 0;
        v2.clear();
        for(int i = 0; i < vx.size(); ++i)
        {
            if(i == 0 || vx[i] > vx[i-1])
                v2.pb(vx[i]);
            else
                ok = 1;
        }
        vx = v2;
        if(ok == 1)
            ++ansmx;
    }
    return ansmx;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> v[i];
    cout << rungg() << '\n';
    return 0;
    while(1)
    {
        n = rand() % 10 + 1;
        for(int i = 1; i <= n; ++i)
            v[i] = i;
        for(int i = 1; i <= n; ++i)
            random_shuffle(v+1, v+n+1);
        int vvv = rungg();
        int zzz = runbrut();
        if(vvv != zzz)
        {
            cout << n << '\n';
            for(int i = 1; i <= n; ++i)
                cout << v[i] << " ";
            cout << '\n';
            cout << vvv << " " << zzz << '\n';
            return 0;
        }
    }
    return 0;
}


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

#define TRACE

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

#define pb push_back
#define ff first
#define ss second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using ll = long long int;
using ld = long double;
using vi = vector<int>;
using mii = map<int, int>;
using pii = pair<int, int>;

const int inf = INT_MAX;
const int N = 100005;
const int mod = 1000000007;

ll power(ll a, ll n){
	ll res = 1;
	//a = a % mod;

	while(n){
		if(n&1) res = res * a;

		n = n >> 1;
		a = a * a;
	}
	return res;
}

int32_t main(){
	fio;
	int n;
	cin >> n;
	int arr[n+2];
	for(int i=1; i<=n; i++) cin >> arr[i];

	stack<int> s;

	int ans[n+2];
	ans[0] = (int)1e6;
	s.push(1);
	for(int i=2; i<=n; i++){
		ans[i] = 1;
		while(!s.empty() and arr[s.top()] < arr[i]){
			ans[i] = max(ans[i], ans[s.top()]+1);
			s.pop();
		}
		s.push(i);
	}
	int res = 0;
	for(int i=1; i<=n; i++){
		if(ans[i]<(int)1e6) res = max(res, ans[i]);
		//cout << ans[i] << " ";
	}
	cout << res << '\n';
	return 0;
}

/*
 */


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

#define TRACE

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

#define pb push_back
#define ff first
#define ss second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using ll = long long int;
using ld = long double;
using vi = vector<int>;
using mii = map<int, int>;
using pii = pair<int, int>;

const int inf = INT_MAX;
const int N = 100005;
const int mod = 1000000007;

ll power(ll a, ll n){
	ll res = 1;
	//a = a % mod;

	while(n){
		if(n&1) res = res * a;

		n = n >> 1;
		a = a * a;
	}
	return res;
}

int32_t main(){
	fio;
	int n;
	cin >> n;
	int arr[n+2];
	for(int i=1; i<=n; i++) cin >> arr[i];

	stack<int> s;

	int ans[n+2];
	ans[0] = (int)1e6;
	s.push(0);
	for(int i=1; i<=n; i++){
		ans[i] = 1;
		while(!s.empty() and arr[s.top()] < arr[i]){
			ans[i] = max(ans[i], ans[s.top()]+1);
			s.pop();
		}
		s.push(i);
	}
	int res = 0;
	for(int i=1; i<=n; i++){
		if(ans[i]<(int)1e6) res = max(res, ans[i]);
		//cout << ans[i] << " ";
	}
	cout << res << '\n';
	return 0;
}

/*
 */


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

#define TRACE

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

#define pb push_back
#define ff first
#define ss second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using ll = long long int;
using ld = long double;
using vi = vector<int>;
using mii = map<int, int>;
using pii = pair<int, int>;

const int inf = INT_MAX;
const int N = 100005;
const int mod = 1000000007;

ll power(ll a, ll n){
	ll res = 1;
	//a = a % mod;

	while(n){
		if(n&1) res = res * a;

		n = n >> 1;
		a = a * a;
	}
	return res;
}

int32_t main(){
	fio;
	int n;
	cin >> n;
	int arr[n+2];
	for(int i=1; i<=n; i++) cin >> arr[i];

	stack<int> s;

	int ans[n+2];
	ans[0] = 1e6;
	s.push(0);
	for(int i=1; i<=n; i++){
		ans[i] = 1;
		while(s.size() and arr[s.top()] < arr[i]){
			ans[i] = max(ans[i], ans[s.top()]+1);
			s.pop();
		}
		s.push(i);
	}
	int res = 0;
	for(int i=1; i<=n; i++){
		if(ans[i]<1e6) res = max(res, ans[i]);
		//cout << ans[i] << " ";
	}
	cout << res << '\n';
	return 0;
}

/*
 */


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

#define TRACE

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

#define pb push_back
#define ff first
#define ss second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using ll = long long int;
using ld = long double;
using vi = vector<int>;
using mii = map<int, int>;
using pii = pair<int, int>;

const int inf = INT_MAX;
const int N = 100005;
const int mod = 1000000007;

ll power(ll a, ll n){
	ll res = 1;
	//a = a % mod;

	while(n){
		if(n&1) res = res * a;

		n = n >> 1;
		a = a * a;
	}
	return res;
}

int32_t main(){
	fio;
	int n;
	cin >> n;
	int arr[n+2];
	for(int i=1; i<=n; i++) cin >> arr[i];

	stack<int> s;

	int ans[n+2];
	ans[1] = (int)1e6;
	s.push(1);
	for(int i=2; i<=n; i++){
		ans[i] = 1;
		while(!s.empty() and arr[s.top()] < arr[i]){
			ans[i] = max(ans[i], ans[s.top()]+1);
			s.pop();
		}
		s.push(i);
	}
	int res = 0;
	for(int i=1; i<=n; i++){
		if(ans[i]<(int)1e6) res = max(res, ans[i]);
		//cout << ans[i] << " ";
	}
	cout << res << '\n';
	return 0;
}

/*
 */


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+20,M=(N<<4),OO=1e9;
#define f first
#define s second
#define pb push_back
#define sz size
int n,A[N],pos[N],tree[M];
void build(int node,int l,int r){
    if(l==r) tree[node]=pos[l];
    else{
        int med=(l+r)/2;
        build(2*node,l,med);
        build(2*node+1,med+1,r);
        tree[node]=max(tree[2*node],tree[2*node+1]);
    }
}
int query(int node,int start,int e,int l,int r){
    if(start>e||start>r||e<l) return 0;
    if(start>=l&&e<=r) return tree[node];
    int med=(start+e)/2;
    int q1=query(2*node,start,med,l,r);
    int q2=query(2*node+1,med+1,e,l,r);
    return max(q1,q2);
}
void update(int p, int l, int r, int idx, int val){
	if(l>r||l>idx||r<idx) return;
	if(l==r)
		tree[p] = val;
	else{
        int med=(l+r)/2;
		update(2*p, l, med, idx, val);
		update(2*p+1, med+1, r, idx, val);
		tree[p] = max(tree[2*p],tree[2*p+1]);
	}
}
stack<pair<int,int>>st;
int main(){
    cin>>n;
    for(int i=0;i<n;++i) cin>>A[i];
    st.push({A[0],0});
    for(int i=1;i<n;++i){
        while(!st.empty()&&st.top().f<=A[i])
            st.pop();
        if(st.empty())
            pos[i]=-1;
        else pos[i]=query(1,0,n-1,st.top().s+1,i-1)+1;
        //if(A[i]==8) cout<<pos[3];
        //cout<<pos[i]<<" ";
        update(1,0,n-1,i,pos[i]);
        st.push({A[i],i});
    }
    //for(int i=0;i<n;++i) cout<<pos[i]<<" ";
    int ans=*max_element(pos,pos+N);
    if(~ans) cout<<ans<<"\n";
    else cout<<"0\n";

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+20,M=(N<<4),OO=1e9;
#define f first
#define s second
#define pb push_back
#define sz size
int n,A[N],pos[N],tree[N];
void build(int node,int l,int r){
    if(l==r) tree[node]=pos[l];
    else{
        int med=(l+r)/2;
        build(2*node,l,med);
        build(2*node+1,med+1,r);
        tree[node]=max(tree[2*node],tree[2*node+1]);
    }
}
int query(int node,int start,int e,int l,int r){
    if(start>e||start>r||e<l) return 0;
    if(start>=l&&e<=r) return tree[node];
    int med=(start+e)/2;
    int q1=query(2*node,start,med,l,r);
    int q2=query(2*node+1,med+1,e,l,r);
    return max(q1,q2);
}
void update(int p, int l, int r, int idx, int val){
	if(l>r||l>idx||r<idx) return;
	if(l==r)
		tree[p] = val;
	else{
        int med=(l+r)/2;
		update(2*p, l, med, idx, val);
		update(2*p+1, med+1, r, idx, val);
		tree[p] = max(tree[2*p],tree[2*p+1]);
	}
}
stack<pair<int,int>>st;
int main(){
    cin>>n;
    for(int i=0;i<n;++i) cin>>A[i];
    st.push({A[0],0});
    for(int i=1;i<n;++i){
        while(!st.empty()&&st.top().f<=A[i])
            st.pop();
        if(st.empty())
            pos[i]=-1;
        else pos[i]=query(1,0,n-1,st.top().s+1,i-1)+1;
        //if(A[i]==8) cout<<pos[3];
        //cout<<pos[i]<<" ";
        update(1,0,n-1,i,pos[i]);
        st.push({A[i],i});
    }
    //for(int i=0;i<n;++i) cout<<pos[i]<<" ";
    int ans=*max_element(pos,pos+N);
    if(~ans) cout<<ans<<"\n";
    else cout<<"0\n";

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+20,M=1e5,OO=1e9;
#define f first
#define s second
#define pb push_back
#define sz size
#define l length
int n,A[N],pos[N];
stack<pair<int,int>>st;
int main(){
    cin>>n;
    for(int i=0;i<n;++i) cin>>A[i];
    st.push({A[0],0});
    for(int i=1;i<n;++i){
        while(!st.empty()&&st.top().f<A[i])
            st.pop();
        if(st.empty())
            pos[i]=-1;
        else pos[i]=i-(st.top().s);
        st.push({A[i],i});
    }
    //for(int i=0;i<n;++i) cout<<pos[i]<<" ";
    int ans=*max_element(pos,pos+N);
    if(~ans) cout<<ans<<"\n";
    else cout<<"0\n";

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+20,M=(N<<5),OO=1e9;
#define f first
#define s second
#define pb push_back
#define sz size
int n,A[N],pos[N],tree[N];
void build(int node,int l,int r){
    if(l==r) tree[node]=pos[l];
    else{
        int med=(l+r)/2;
        build(2*node,l,med);
        build(2*node+1,med+1,r);
        tree[node]=max(tree[2*node],tree[2*node+1]);
    }
}
int query(int node,int start,int e,int l,int r){
    if(start>e||start>r||e<l) return 0;
    if(start>=l&&e<=r) return tree[node];
    int med=(start+e)/2;
    int q1=query(2*node,start,med,l,r);
    int q2=query(2*node+1,med+1,e,l,r);
    return max(q1,q2);
}
void update(int p, int l, int r, int idx, int val){
	if(l>r||l>idx||r<idx) return;
	if(l==r)
		tree[p] = val;
	else{
        int med=(l+r)/2;
		update(2*p, l, med, idx, val);
		update(2*p+1, med+1, r, idx, val);
		tree[p] = max(tree[2*p],tree[2*p+1]);
	}
}
stack<pair<int,int>>st;
int main(){
    cin>>n;
    for(int i=0;i<n;++i) cin>>A[i];
    st.push({A[0],0});
    for(int i=1;i<n;++i){
        while(!st.empty()&&st.top().f<=A[i])
            st.pop();
        if(st.empty())
            pos[i]=-1;
        else pos[i]=query(1,0,n-1,st.top().s+1,i-1)+1;
        //if(A[i]==8) cout<<pos[3];
        //cout<<pos[i]<<" ";
        update(1,0,n-1,i,pos[i]);
        st.push({A[i],i});
    }
    //for(int i=0;i<n;++i) cout<<pos[i]<<" ";
    int ans=*max_element(pos,pos+N);
    if(~ans) cout<<ans<<"\n";
    else cout<<"0\n";

}


#include <bits/stdc++.h>

using namespace std;

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

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    //cout << 1;
    stack<pair<int,int>> prvi;
    stack<pair<int,int>> drugi;
    prvi.push(make_pair(a[0], 0));
    //cout << 1;
    int maks = 0, br = 1;
    bool f = false;
    for(int i = 1; i < n; i++)
    {
        //cout << i;
        f = false;
        br = 1;
        if(drugi.empty())
        {
            if(prvi.top().first < a[i])
            {
                prvi.pop();
                prvi.push(make_pair(a[i],br));
            }
            else
            {
                drugi.push(make_pair(a[i], br));
            }
            continue;
        }

        while(!drugi.empty())
        {
            //cout << i;
            if(drugi.top().first > a[i])
            {
                drugi.pop();
                drugi.push(make_pair(a[i], br));
                if(br > maks)
                    maks = br;
                break;
            }
            else
            {
                while(drugi.top().first < a[i])
                {
                    //if(drugi.top().first == 14)
                        //cout << drugi.top().second;
                    br = max(br, drugi.top().second);
                    drugi.pop();
                    if(drugi.empty())
                    {
                        if(prvi.top().first < a[i])
                        {
                            f = true;
                            prvi.pop();
                            prvi.push(make_pair(a[i], br));
                            //br++;
                        }
                        else
                        {
                            br++;
                        }
                        break;
                    }
                }
                //cout << i;
                if(!f)
                {
                    if(br == 5)
                        cout << a[i];
                    if(br > maks)
                        maks = br;
                    for(int j = 0; j < br; j++)
                        drugi.push(make_pair(a[i], br));
                }
                break;
            }
        }
    }

    cout << maks;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    stack<pair<int, int>> stek;
    stek.push(make_pair(a[0], n));
    int maks = 0;
    for(int i = 1; i < n; i++)
    {
        int br = 1;
        while(!stek.empty())
        {
            if(stek.top().first > a[i])
            {
                stek.top().second = stek.top().second - 1;
                if(stek.top().second == 0)
                    stek.pop();
                stek.push(make_pair(a[i], br));
                if(br > maks)
                    maks = br;
                break;
            }
            else
            {
                br += stek.top().second;
                stek.pop();
                if(stek.empty())
                {
                    stek.push(make_pair(a[i], n));
                    break;
                }
            }
        }
        //cout << i;
    }
    cout << maks;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    //cout << 1;
    stack<pair<int,int>> prvi;
    stack<pair<int,int>> drugi;
    prvi.push(make_pair(a[0], 0));
    //cout << 1;
    int maks = 0, br = 1;
    bool f = false;
    for(int i = 1; i < n; i++)
    {
        //cout << i;
        f = false;
        br = 1;
        if(drugi.empty())
        {
            if(prvi.top().first < a[i])
            {
                prvi.pop();
                prvi.push(make_pair(a[i],br));
            }
            else
            {
                drugi.push(make_pair(a[i], br));
            }
            continue;
        }

        while(!drugi.empty())
        {
            //cout << i;
            if(drugi.top().first > a[i])
            {
                drugi.pop();
                drugi.push(make_pair(a[i], br));
                break;
            }
            else
            {
                while(drugi.top().first < a[i])
                {
                    //cout << i;
                    br += 1;
                    drugi.pop();
                   // if(a[i] == 5)
                   // {
                   //    cout << "f" << drugi.top().first << 'f';
                   // }

                    if(drugi.empty())
                    {
                        if(prvi.top().first < a[i])
                        {
                            f = true;
                            prvi.pop();
                            prvi.push(make_pair(a[i], br));
                        }
                        break;
                    }
                }
                //cout << i;
                if(!f)
                {
                    //if(a[i] == 4)
                       // cout << br;
                if(br > maks)
                    maks = br;
                for(int j = 0; j < br; j++)
                    drugi.push(make_pair(a[i], br));
                }
                break;
            }
        }
    }

    cout << maks;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    //cout << 1;
    stack<pair<int,int>> prvi;
    stack<pair<int,int>> drugi;
    prvi.push(make_pair(a[0], 0));
    //cout << 1;
    int maks = 0, br = 1;
    bool f = false;
    for(int i = 1; i < n; i++)
    {
        //cout << i;
        f = false;
        br = 1;
        if(drugi.empty())
        {
            if(prvi.top().first < a[i])
            {
                prvi.pop();
                prvi.push(make_pair(a[i],br));
            }
            else
            {
                drugi.push(make_pair(a[i], br));
            }
            continue;
        }

        while(!drugi.empty())
        {
            //cout << i;
            if(drugi.top().first > a[i])
            {
                drugi.pop();
                drugi.push(make_pair(a[i], br));
                if(br > maks)
                    maks = br;
                break;
            }
            else
            {
                while(drugi.top().first < a[i])
                {
                    //cout << i;
                    br += 1;
                    drugi.pop();
                   // if(a[i] == 5)
                   // {
                   //    cout << "f" << drugi.top().first << 'f';
                   // }

                    if(drugi.empty())
                    {
                        if(prvi.top().first < a[i])
                        {
                            f = true;
                            prvi.pop();
                            prvi.push(make_pair(a[i], br));
                        }
                        break;
                    }
                }
                //cout << i;
                if(!f)
                {
                    //if(a[i] == 4)
                       // cout << br;
                if(br > maks)
                    maks = br;
                for(int j = 0; j < br; j++)
                    drugi.push(make_pair(a[i], br));
                }
                break;
            }
        }
    }

    cout << maks;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    //cout << 1;
    stack<pair<int,int>> prvi;
    stack<pair<int,int>> drugi;
    prvi.push(make_pair(a[0], 0));
    //cout << 1;
    int maks = 0, br = 1;
    bool f = false;
    for(int i = 1; i < n; i++)
    {
        //cout << i;
        f = false;
        br = 1;
        if(drugi.empty())
        {
            if(prvi.top().first < a[i])
            {
                prvi.pop();
                prvi.push(make_pair(a[i],br));
            }
            else
            {
                drugi.push(make_pair(a[i], br+1));
            }
            continue;
        }

        while(!drugi.empty())
        {
            //cout << i;
            if(drugi.top().first > a[i])
            {
                drugi.pop();
                drugi.push(make_pair(a[i], br));
                if(br > maks)
                    maks = br;
                break;
            }
            else
            {
                while(drugi.top().first < a[i])
                {
                    //if(drugi.top().first == 14)
                        //cout << drugi.top().second;
                    br = max(br, drugi.top().second);
                    drugi.pop();
                    if(drugi.empty())
                    {
                        if(prvi.top().first < a[i])
                        {
                            f = true;
                            prvi.pop();
                            prvi.push(make_pair(a[i], br));
                            //br++;
                        }
                        else
                        {
                            br++;
                           // if(br > maks)
                                //maks = br;
                        }
                        break;
                    }
                }
                //cout << i;
                if(!drugi.empty())br++;
                if(!f)
                {
                    //if(a[i] == 13)
                        //cout << br;
                    if(br > maks)
                        maks = br;
                    for(int j = 0; j < br; j++)
                        drugi.push(make_pair(a[i], br));
                }
                break;
            }
        }
    }

    cout << maks;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 1; i <= n; i++)
#define pb push_back
const ll M = 1e5+10;
const ll INF=1e15+5;
const ll MOD = 1e9+7;
const ll N=2e5;
const double PI= 3.14159265358979323846;
#define mp make_pair
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi,pi> piii;
typedef vector<ll> vi;
typedef vector<pi> vii;
typedef vector< vi > matrix;
ll n,id[M],arr[M];
vi v;

void init()
{
  rep(i,n) arr[i]=i;
}

ll root(ll i)
{
  while(i!=arr[i])
  {
    arr[i]=arr[arr[i]];
    i=arr[i];
  }
  return i;
}

void merg(ll x,ll y)
{
  ll px=root(x);
  ll py=root(y);
  arr[px]=py;
}

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

  cin >> n;
  rep(i,n) cin >> id[i];
  init();
  ll k=0;
  rep(i,n-1)
  {
    if(id[i]>id[i+1])
      merg(i,i+1),k++;
    v.pb(i);
  }
  v.pb(n);

  ll u,ans=0;
  while(k)
  {
    u=1;
    ans++;
    v.clear();
    v.pb(1);
    u=root(u)+1;
    //cout << u << endl;
    while(u!=(n+1))
    {
      v.pb(u);
      u=root(u)+1;
      //cout<< u << endl;
    }
    k=0;
    for(ll i=1;i<=v.size();i++)
      arr[i]=i,id[i]=id[v[i-1]];
    for(ll i=1;i<v.size();i++)
      if(id[i]>id[i+1])
        merg(i,i+1),k++;
    n=v.size();
    //cout << "n is " << n << endl;
  }
  cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 1; i <= n; i++)
#define pb push_back
const ll M = 1e5+10;
const ll INF=1e15+5;
const ll MOD = 1e9+7;
const ll N=2e5;
const double PI= 3.14159265358979323846;
#define mp make_pair
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi,pi> piii;
typedef vector<ll> vi;
typedef vector<pi> vii;
typedef vector< vi > matrix;
ll n,id[M],arr[M];
vi v;

void init()
{
  rep(i,n) arr[i]=i;
}

ll root(ll i)
{
  while(i!=arr[i])
  {
    arr[i]=arr[arr[i]];
    i=arr[i];
  }
  return i;
}

void merg(ll x,ll y)
{
  ll px=root(x);
  ll py=root(y);
  arr[px]=py;
}

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

  cin >> n;
  rep(i,n) cin >> id[i];
  init();
  ll k=0;
  rep(i,n-1)
  {
    if(id[i]>id[i+1])
      merg(i,i+1),k++;
    v.pb(i);
  }
  v.pb(n);

  ll u,ans=0;
  while(k)
  {
    u=1;
    ans++;
    v.clear();
    v.pb(1);
    u=root(u)+1;
    //cout << u << endl;
    while(u!=(n+1))
    {
      v.pb(u);
      u=root(u)+1;
      //cout<< u << endl;
    }
    k=0;
    for(ll i=0;i<=v.size()-1;i++)
      arr[v[i]]=v[i];
    for(ll i=0;i<v.size()-1;i++)
      if(id[v[i]]>id[v[i+1]])
        merg(v[i],v[i+1]),k++;
    n=v[v.size()-1];
    //cout << "n is " << n << endl;
  }
  cout << ans << "\n";
}


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int MAXN = 100000;
int val[MAXN];
int tdeath[MAXN];

int main() {
    cin.sync_with_stdio(false);
    int n;
    scanf("%d", &n);
    for (int i=0; i<n; ++i) {
        scanf("%d", val+i);
    }

    tdeath[0] = -1;
    vector<int> S(1, 0);
    int sol = 0;
    for (int i=1; i<n; ++i) {
        tdeath[i] = 0;
        while (S.size()>0 && val[S.back()]<val[i]) {
            tdeath[i] = max(tdeath[i], tdeath[S.back()]+1);
            S.pop_back();
        }
        if (S.size() == 0) {
            tdeath[i] = -1;
        }
        S.push_back(i);
        sol = max(sol, tdeath[i]+1);
    }

    printf("%d\n", sol);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 1; i <= n; i++)
#define pb push_back
const ll M = 1e5+10;
const ll INF=1e15+5;
const ll MOD = 1e9+7;
const ll N=2e5;
const double PI= 3.14159265358979323846;
#define mp make_pair
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi,pi> piii;
typedef vector<ll> vi;
typedef vector<pi> vii;
typedef vector< vi > matrix;
ll n,id[M],arr[M];
vi v;

void init()
{
  rep(i,n) arr[i]=i;
}

ll root(ll i)
{
  while(i!=arr[i])
  {
    arr[i]=arr[arr[i]];
    i=arr[i];
  }
  return i;
}

void merg(ll x,ll y)
{
  ll px=root(x);
  ll py=root(y);
  arr[px]=py;
}

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

  cin >> n;
  rep(i,n) cin >> id[i];
  init();
  ll k=0;
  rep(i,n-1)
  {
    if(id[i]>id[i+1])
      merg(i,i+1),k++;
    v.pb(i);
  }
  v.pb(n);

  ll u,ans=0;
  while(k)
  {
    u=1;
    ans++;
    v.clear();
    v.pb(1);
    u=root(u)+1;
    //cout << u << endl;
    while(u!=(n+1))
    {
      v.pb(u);
      u=root(u)+1;
      //cout<< u << endl;
    }
    k=0;
    for(ll i=1;i<=v.size();i++)
      arr[i]=i,id[i]=id[v[i-1]];
    for(ll i=0;i<v.size()-1;i++)
      if(id[v[i]]>id[v[i+1]])
        merg(i+1,i+2),k++;
    n=v.size();
    //cout << "n is " << n << endl;
  }
  cout << ans << "\n";
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   vector<int>v;
   for(int i=0;i<n;i++){
       int a; cin>>a; v.pb(a);
   }
   int f=0,c=0;
   while(f==0 && v.size()>1){
       c++;
       vector<int>v1,v2;
       f=1;
       v1.pb(v[0]);
       for(int i=1;i<v.size();i++){
           if(v[i]>v[i-1]){v1.pb(v[i]);}
           else{f=0;}
       }
       v2=v1;
       v1.clear();
       v.clear();
       v=v2;
   }
    cout<<c-(v.size()!=0)<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   vector<int>v;
   for(int i=0;i<n;i++){
       int a; cin>>a; v.pb(a);
   }
   int f=0,c=0;
   while(f==0 && v.size()>1){
       c++;
       vector<int>v1,v2;
       f=1;
       v1.pb(v[0]);
       for(int i=1;i<v.size();i++){
           if(v[i]>v[i-1]){v1.pb(v[i]);}
           else{f=0;}
       }
       v2=v1;
       v1.clear();
       v.clear();
       v=v2;
   }
    cout<<c-(v.size()==n)<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   ll a[n];
   stack<int>st,st1;

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

   st.push(0);
   ll d[n+1],c=0; d[0]=0;
   for(int i=1;i<n;i++){
     d[i]=1;
     while(!st.empty() && a[st.top()]<a[i]){
         d[i]=max(d[i],d[st.top()]+1);
         st.pop();
     }
     if(st.empty()){d[i]=0;}
     st.push(i);
   }
   ll ans=INT_MIN; for(int i=0;i<n;i++){
       //cout<<d[i]<<" ";
       ans=max(ans,d[i]);}
   cout<<ans<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   ll a[n];
   stack<int>st,st1;

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

   st.push(0);
   ll d[n+1],c=0; d[0]=0;
   for(int i=1;i<n;i++){
     d[i]=0;
     while(!st.empty() && a[st.top()]<a[i]){
         d[i]=max(d[i],d[st.top()]+1);
         st.pop();
     }
     if(st.empty()){d[i]=0;}
     st.push(i);
   }
   ll ans=INT_MIN; for(int i=0;i<n;i++){
       //cout<<d[i]<<" ";
       ans=max(ans,d[i]);}
   cout<<ans<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   ll a[n];
   stack<int>st,st1;

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

   st.push(0);
   ll d[n+1],c=0; d[0]=0;
   for(int i=1;i<n;i++){
     d[i]=1;
     while(!st.empty() && a[st.top()]<a[i]){
         d[i]=max(d[i],d[st.top()]+1);
         st.pop();
     }
     if(st.empty()){d[i]=0;}
     st.push(i);
   }

   cout<<*max_element(d,d+n)<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   ll a[n];
   stack<int>st,st1;

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

   st.push(0);
   ll d[n+1],c=0; d[0]=1;
   for(int i=1;i<n;i++){
     d[i]=1;
     while(!st.empty() && a[st.top()]<a[i]){
         d[i]=max(d[i],d[st.top()]+1);
         st.pop();
     }
     if(st.empty()){d[i]=1;}
     st.push(i);
   }
   ll ans=INT_MIN; for(int i=0;i<n;i++){
       //cout<<d[i]<<" ";
       ans=max(ans,d[i]);}
   cout<<ans<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   ll a[n];
   stack<int>st,st1;

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

   st.push(0);
   ll d[n+1],c=0; d[0]=0;
   for(int i=1;i<n;i++){
     d[i]=0;
     while(!st.empty() && a[st.top()]<a[i]){
         d[i]=max(d[i],d[st.top()]+1);
         st.pop();
     }
     if(st.empty()){d[i]=-1;}
     st.push(i);
   }
   ll ans=INT_MIN; for(int i=0;i<n;i++){
       //cout<<d[i]<<" ";
       ans=max(ans,d[i]);}
   cout<<ans+1<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   ll a[n];
   stack<int>st,st1;

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

   st.push(0);
   ll d[n+1],c=0; d[0]=-1;
   for(int i=1;i<n;i++){
     d[i]=0;
     while(!st.empty() && a[st.top()]<a[i]){
         d[i]=max(d[i],d[st.top()]+1);
         st.pop();
     }
     if(st.empty()){d[i]=-1;}
     st.push(i);
   }
   ll ans=INT_MIN; for(int i=0;i<n;i++){
       //cout<<d[i]<<" ";
       ans=max(ans,d[i]);}
   cout<<ans+1<<endl;

   return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f first
#define s second

using namespace std;

int main() {

   ll n; cin>>n;
   vector<int>v;
   for(int i=0;i<n;i++){
       int a; cin>>a; v.pb(a);
   }
   int f=0,c=0;
   while(f==0 && v.size()>1){
       c++;
       vector<int>v1,v2;
       f=1;
       v1.pb(v[0]);
       for(int i=1;i<v.size();i++){
           if(v[i]>v[i-1]){v1.pb(v[i]);}
           else{f=0;}
       }
       v2=v1;
       v1.clear();
       v.clear();
       v=v2;
   }
    cout<<c-(v.size()!=1)<<endl;

   return 0;
}


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

const int N = 100 * 1000 + 20, inf = 1000 * 1000;

int par[N], dt[N];
vector<int> v;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	iota(par, par + n, -1);
	for (int i = 0; i < n; i++) {
		int x = i;
		while(par[x] != -1 and v[par[x]] < v[i]) {
			x = par[x];
		}
		if (par[x] == -1) {
			dt[i] = inf;
		}
		else {
			dt[i] = dt[x] + 1;
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (dt[i] < inf) {
			ans = max(ans, dt[i]);
		}
	}
	cout << ans;
	return 0;
}


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

const int N = 100 * 1000 + 20, inf = 1000 * 1000;

int par[N], dt[N];
vector<int> v;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	iota(par, par + n, -1);
	for (int i = 0; i < n; i++) {
		int y = 0;
		int x = i;
		while(par[x] != -1 and v[par[x]] < v[i]) {
			y = max(y, dt[x]);
			x = par[x];
		}
		if (par[x] == -1) {
			dt[i] = inf;
		}
		else {
			dt[i] = y + 1;
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (dt[i] < inf) {
			ans = max(ans, dt[i]);
		}
	}
	cout << ans;
	return 0;
}


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

const int N = 100 * 1000 + 20, inf = 1000 * 1000;

int par[N], dt[N];
vector<int> v;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	iota(par, par + n, -1);
	for (int i = 0; i < n; i++) {
//		cout << "i = " << i << " : ";
		int y = 0;
		int x = i;
//		cout << "x = " << x << ", " << "par = " << par[x] << ", v = " << v[par[x]] << "/ " << v[i] << endl;
		while(par[x] != -1 and v[par[x]] < v[i]) {
			x = par[x];
			y = max(y, dt[x]);
		}
		if (par[x] == -1) {
			dt[i] = inf;
			par[i] = -1;
		}
		else {
			dt[i] = y + 1;
			par[i] = par[x];
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
//		cout << "i = " << i << " : " << dt[i] << ", " << par[i] << "\n";
		if (dt[i] < inf) {
			ans = max(ans, dt[i]);
		}
	}
	cout << ans;
	return 0;
}


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

const int N = 100 * 1000 + 20, inf = 1000 * 1000;

int par[N], dt[N];
vector<int> v;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	iota(par, par + n, -1);
	for (int i = 0; i < n; i++) {
//		cout << "i = " << i << " : ";
		int y = 0;
		int x = i;
//		cout << "x = " << x << ", " << "par = " << par[x] << ", v = " << v[par[x]] << "/ " << v[i] << endl;
		while(par[x] != -1 and v[par[x]] < v[i]) {
			x = par[x];
			y = max(y, dt[x]);
		}
		if (par[x] == -1) {
			dt[i] = inf;
			par[i] = -1;
		}
		else {
			dt[i] = y + 1;
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
//		cout << "i = " << i << " : " << dt[i] << ", " << par[i] << "\n";
		if (dt[i] < inf) {
			ans = max(ans, dt[i]);
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define read(type) readInt<type>()
#define gc getchar
template <typename T> inline void write(T x)
{
	int i = 20;
	char buf[21];
	// buf[10] = 0;
	buf[20] = '\n';

	do
	{
		buf[--i] = x % 10 + '0';
		x/= 10;
	}while(x);
	do
	{
		putchar(buf[i]);
	} while (buf[i++] != '\n');
}

template <typename T> inline T readInt()
{
	T n=0,s=1;
	char p=gc();
	if(p=='-')
		s=-1;
	while((p<'0'||p>'9')&&p!=EOF&&p!='-')
		p=gc();
	if(p=='-')
		s=-1,p=gc();
	while(p>='0'&&p<='9') {
		n = (n<< 3) + (n<< 1) + (p - '0');
		p=gc();
	}

	return n*s;
}

int main()
{

	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	#endif
	//begin
	long long n = read(long long);
	long long a[n];
	for(int i=0;i<n;i++){
		a[i] = read(long long);
	}
	queue<long long > q1,q2;
	for(int i=n-1;i>=0;i--)
		q1.push(a[i]);
	long long ans = 0;
	while(true){
		if(q1.empty())
			break;
		long long prevsz = q1.size() , cur = q1.front();
		q1.pop();
		for(int i=0;i<prevsz;i++){
			if(cur < q1.front()){
				cur = q1.front();
				q1.pop();
			}else if(cur >= q1.front()){
				q2.push(cur);
				cur = q1.front();
				q1.pop();
			}
		}
		if(q2.size() == prevsz)
			break;
		ans++;
		while(!q2.empty()){
			//cout<<q2.front()<<" ";
			q1.push(q2.front());
			q2.pop();
		}
		//cout<<endl;
	}
	if(ans == 0)
		ans++;
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define read(type) readInt<type>()
#define gc getchar
template <typename T> inline void write(T x)
{
	int i = 20;
	char buf[21];
	// buf[10] = 0;
	buf[20] = '\n';

	do
	{
		buf[--i] = x % 10 + '0';
		x/= 10;
	}while(x);
	do
	{
		putchar(buf[i]);
	} while (buf[i++] != '\n');
}

template <typename T> inline T readInt()
{
	T n=0,s=1;
	char p=gc();
	if(p=='-')
		s=-1;
	while((p<'0'||p>'9')&&p!=EOF&&p!='-')
		p=gc();
	if(p=='-')
		s=-1,p=gc();
	while(p>='0'&&p<='9') {
		n = (n<< 3) + (n<< 1) + (p - '0');
		p=gc();
	}

	return n*s;
}

int main()
{

	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	#endif
	//begin
	long long n = read(long long);
	long long a[n];
	for(int i=0;i<n;i++){
		a[i] = read(long long);
	}
	queue<long long > q1,q2;
	for(int i=n-1;i>=0;i--)
		q1.push(a[i]);
	long long ans = 0;
	while(true){
		if(q1.empty())
			break;
		long long prevsz = q1.size() , cur = q1.front();
		q1.pop();
		for(int i=0;i<prevsz;i++){
			if(cur < q1.front()){
				cur = q1.front();
				q1.pop();
			}else if(cur >= q1.front()){
				q2.push(cur);
				cur = q1.front();
				q1.pop();
			}
		}
		if(q2.size() == prevsz)
			break;
		ans++;
		while(!q2.empty()){
			//cout<<q2.front()<<" ";
			q1.push(q2.front());
			q2.pop();
		}
		//cout<<endl;
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define read(type) readInt<type>()
#define gc getchar
template <typename T> inline void write(T x)
{
	int i = 20;
	char buf[21];
	// buf[10] = 0;
	buf[20] = '\n';

	do
	{
		buf[--i] = x % 10 + '0';
		x/= 10;
	}while(x);
	do
	{
		putchar(buf[i]);
	} while (buf[i++] != '\n');
}

template <typename T> inline T readInt()
{
	T n=0,s=1;
	char p=gc();
	if(p=='-')
		s=-1;
	while((p<'0'||p>'9')&&p!=EOF&&p!='-')
		p=gc();
	if(p=='-')
		s=-1,p=gc();
	while(p>='0'&&p<='9') {
		n = (n<< 3) + (n<< 1) + (p - '0');
		p=gc();
	}

	return n*s;
}

int main()
{

	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	#endif
	//begin
	long long n = read(long long);
	long long a[n];
	for(int i=0;i<n;i++){
		a[i] = read(long long);
	}
	queue<long long > q1,q2;
	for(int i=n-1;i>=0;i--)
		q1.push(a[i]);
	long long ans = 0;
	while(true){
		if(q1.empty())
			break;
		long long prevsz = q1.size() , cur = q1.front();
		q1.pop();
		for(int i=0;i<prevsz;i++){
			if(cur < q1.front()){
				cur = q1.front();
				q1.pop();
			}else if(cur >= q1.front()){
				q2.push(cur);
				cur = q1.front();
				q1.pop();
			}
		}
		if(q2.size() == prevsz)
			break;
		ans++;
		while(!q2.empty()){
			//cout<<q2.front()<<" ";
			q1.push(q2.front());
			q2.pop();
		}
		//cout<<endl;
	}
	//if(ans == 0)
	//	ans++;
	cout<<ans<<endl;
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1000005
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

int a[maxn];
int f[maxn];
int t[maxn];
int main()
{
    int n=read(),top=0,ans=0;
    for(int i=0;i<n;i++)
        a[i]=read();
    for(int i=n-1;i>=0;i--)
    {
        int tt=0;
        while(top&&a[t[top-1]]<a[i])
            f[i]=tt=max(tt+1,f[t[--top]]);
        t[top++]=i;
    }
    printf("%d",*max_element(f,f+n));
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define read(type) readInt<type>()
#define gc getchar
template <typename T> inline void write(T x)
{
	int i = 20;
	char buf[21];
	// buf[10] = 0;
	buf[20] = '\n';

	do
	{
		buf[--i] = x % 10 + '0';
		x/= 10;
	}while(x);
	do
	{
		putchar(buf[i]);
	} while (buf[i++] != '\n');
}

template <typename T> inline T readInt()
{
	T n=0,s=1;
	char p=gc();
	if(p=='-')
		s=-1;
	while((p<'0'||p>'9')&&p!=EOF&&p!='-')
		p=gc();
	if(p=='-')
		s=-1,p=gc();
	while(p>='0'&&p<='9') {
		n = (n<< 3) + (n<< 1) + (p - '0');
		p=gc();
	}

	return n*s;
}

int main()
{

	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	#endif
	//begin
	long long n = read(long long);
	long long a[n+10];
	for(int i=0;i<n;i++){
		a[i] = read(long long);
	}
	queue<long long > q1,q2;
	for(int i=n-1;i>=0;i--)
		q1.push(a[i]);
	long long ans = 0;
	while(true){
		if(q1.empty())
			break;
		long long sz = q1.size() , cur = q1.front();
		q1.pop();
		for(int i=0;i < sz;i++){
			if(cur < q1.front()){
				cur = q1.front();
				q1.pop();
			}else if(cur >= q1.front()){
				q2.push(cur);
				cur = q1.front();
				q1.pop();
			}
		}
		if(q2.size() == sz)
			break;
		ans++;
		while(!q2.empty()){
			//cout<<q2.front()<<" ";
			q1.push(q2.front());
			q2.pop();
		}
		//cout<<endl;
	}
	//if(ans == 0)
	//	ans++;
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define read(type) readInt<type>()
#define gc getchar
template <typename T> inline void write(T x)
{
	int i = 20;
	char buf[21];
	// buf[10] = 0;
	buf[20] = '\n';

	do
	{
		buf[--i] = x % 10 + '0';
		x/= 10;
	}while(x);
	do
	{
		putchar(buf[i]);
	} while (buf[i++] != '\n');
}

template <typename T> inline T readInt()
{
	T n=0,s=1;
	char p=gc();
	if(p=='-')
		s=-1;
	while((p<'0'||p>'9')&&p!=EOF&&p!='-')
		p=gc();
	if(p=='-')
		s=-1,p=gc();
	while(p>='0'&&p<='9') {
		n = (n<< 3) + (n<< 1) + (p - '0');
		p=gc();
	}

	return n*s;
}

void clear( queue<long long> &q )
{
   queue<long long> empty;
   swap( q, empty );
}

int main()
{

	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	#endif
	//begin
	long long n = read(long long);
	long long a[n+10];
	for(int i=0;i<n;i++){
		a[i] = read(long long);
	}
	queue<long long > q1,q2;
	for(int i=n-1;i>=0;i--)
		q1.push(a[i]);
	long long ans = 0;
	while(true){
		if(q1.empty())
			break;
		long long sz = q1.size() , cur = q1.front();
		q1.pop();
		for(int i=0;i < sz;i++){
			if(cur < q1.front()){
				cur = q1.front();
				q1.pop();
			}else if(cur >= q1.front()){
				q2.push(cur);
				cur = q1.front();
				q1.pop();
			}
		}
		if(q2.size() == sz)
			break;
		ans++;
		clear(q1);
		while(!q2.empty()){
			//cout<<q2.front()<<" ";
			q1.push(q2.front());
			q2.pop();
		}
		clear(q2);
	}
	//if(ans == 0)
	//	ans++;
	cout<<ans;
	return 0;
}


/*
author: orthodoxparadox
15-Dec-2018
04:48:01
*/

#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 std;
using namespace __gnu_pbds;

#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define fup(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define fnu for(ll i = 0; i < n; i++)
#define rev(Y) reverse(all(Y))
#define oiint ostream_iterator<int>(cout, " ")
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define dbg(...)
#endif
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}};
const int mod = 1e9 + 7;

int takemod(int a){return ((a%mod)+mod)%mod;}

int fast_exp(int base, int expo) {
	int res=1;
	while(expo>0) {
		if(expo&1) res=(res*base)%mod;
		base=(base*base)%mod;
		expo>>=1;}
	return res;}

int modinv(int a){return takemod(fast_exp(takemod(a), mod-2));}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef __APPLE__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
	time_t t1, t2;
	t1 = clock();

	int n;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; i++) cin >> a[i];
	int cnt = 1;
	int maxcnt = 1;
	vector<int> v;
	int cur = a[0];
	v.pb(cur);
	for(int i = 1; i < n; i++)
	{
		if(a[i] > a[i-1])
		{
			maxcnt = max(maxcnt, cnt);
			v.pb(a[i]);
			cnt = 1;
			continue;
		}
		cnt++;
		dbg(cnt);
		max(maxcnt, cnt);
	}
	int steps = 0;
	while(maxcnt != 1)
	{
		dbg(v);
		steps++;
		cnt = 1;
		maxcnt = 1;
		int n = v.size();
		vector<int> res;
		int cur = v[0];
		res.pb(cur);
		for (int i = 1; i < n; i++)
		{
			if (v[i] > v[i - 1])
			{
				maxcnt = max(maxcnt, cnt);
				res.pb(v[i]);
				cnt = 1;
				continue;
			}
			cnt++;
			dbg(cnt);
			max(maxcnt, cnt);
		}
		maxcnt = max(maxcnt, cnt);
		dbg(maxcnt);
		v = res;
	}
	cout << steps;
	t2 = clock(); cerr << "time taken: " << t2-t1;
	return 0;
}


/*
author: orthodoxparadox
15-Dec-2018
04:48:01
*/

#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 std;
using namespace __gnu_pbds;

#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define fup(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define fnu for(ll i = 0; i < n; i++)
#define rev(Y) reverse(all(Y))
#define oiint ostream_iterator<int>(cout, " ")
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define dbg(...)
#endif
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}};
const int mod = 1e9 + 7;

int takemod(int a){return ((a%mod)+mod)%mod;}

int fast_exp(int base, int expo) {
	int res=1;
	while(expo>0) {
		if(expo&1) res=(res*base)%mod;
		base=(base*base)%mod;
		expo>>=1;}
	return res;}

int modinv(int a){return takemod(fast_exp(takemod(a), mod-2));}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef __APPLE__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
	time_t t1, t2;
	t1 = clock();

	int n;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; i++) cin >> a[i];
	int cnt = 1;
	int maxcnt = 1;
	vector<int> v;
	int cur = a[0];
	v.pb(cur);
	for(int i = 1; i < n; i++)
	{
		if(a[i] > a[i-1])
		{
			maxcnt = max(maxcnt, cnt);
			if(i != n-1 && a[i + 1] < a[i])
				v.pb(a[i]);
			cnt = 1;
			continue;
		}
		cnt++;
		dbg(cnt);
		max(maxcnt, cnt);
	}
	maxcnt = max(maxcnt, cnt);
	int steps = 0;
	while(maxcnt != 1)
	{
		dbg(v);
		steps++;
		cnt = 1;
		maxcnt = 1;
		int n = v.size();
		vector<int> res;
		int cur = v[0];
		res.pb(cur);
		for (int i = 1; i < n; i++)
		{
			if (v[i] > v[i - 1])
			{
				maxcnt = max(maxcnt, cnt);
				if(i != n-1 && v[i+1] < v[i])
					res.pb(v[i]);
				cnt = 1;
				continue;
			}
			cnt++;
			dbg(cnt);
			max(maxcnt, cnt);
		}
		maxcnt = max(maxcnt, cnt);
		dbg(maxcnt);
		v = res;
	}
	cout << steps;
	t2 = clock(); cerr << "time taken: " << t2-t1;
	return 0;
}


/*
author: orthodoxparadox
15-Dec-2018
04:48:01
*/

#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 std;
using namespace __gnu_pbds;

#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define fup(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define fnu for(ll i = 0; i < n; i++)
#define rev(Y) reverse(all(Y))
#define oiint ostream_iterator<int>(cout, " ")
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define dbg(...)
#endif
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}};
const int mod = 1e9 + 7;

int takemod(int a){return ((a%mod)+mod)%mod;}

int fast_exp(int base, int expo) {
	int res=1;
	while(expo>0) {
		if(expo&1) res=(res*base)%mod;
		base=(base*base)%mod;
		expo>>=1;}
	return res;}

int modinv(int a){return takemod(fast_exp(takemod(a), mod-2));}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef __APPLE__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
	time_t t1, t2;
	t1 = clock();

	int n;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; i++) cin >> a[i];
	int cnt = 1;
	int maxcnt = 1;
	vector<int> v;
	int cur = a[0];
	v.pb(cur);
	for(int i = 1; i < n; i++)
	{
		if(a[i] > a[i-1])
		{
			maxcnt = max(maxcnt, cnt);
			v.pb(a[i]);
			cnt = 1;
			continue;
		}
		cnt++;
		dbg(cnt);
		max(maxcnt, cnt);
	}
	maxcnt = max(maxcnt, cnt);
	int steps = 0;
	while(maxcnt != 1)
	{
		dbg(v);
		steps++;
		cnt = 1;
		maxcnt = 1;
		int n = v.size();
		vector<int> res;
		int cur = v[0];
		res.pb(cur);
		for (int i = 1; i < n; i++)
		{
			if (v[i] > v[i - 1])
			{
				maxcnt = max(maxcnt, cnt);
				res.pb(v[i]);
				cnt = 1;
				continue;
			}
			cnt++;
			dbg(cnt);
			max(maxcnt, cnt);
		}
		maxcnt = max(maxcnt, cnt);
		dbg(maxcnt);
		v = res;
	}
	cout << steps;
	t2 = clock(); cerr << "time taken: " << t2-t1;
	return 0;
}


/*
author: orthodoxparadox
15-Dec-2018
07:26:09
*/

#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 std;
using namespace __gnu_pbds;

#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define fup(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define fnu for(ll i = 0; i < n; i++)
#define rev(Y) reverse(all(Y))
#define oiint ostream_iterator<int>(cout, " ")
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define dbg(...)
#endif
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}};
const int mod = 1e9 + 7;

int takemod(int a){return ((a%mod)+mod)%mod;}

int fast_exp(int base, int expo) {
	int res=1;
	while(expo>0) {
		if(expo&1) res=(res*base)%mod;
		base=(base*base)%mod;
		expo>>=1;}
	return res;}

int modinv(int a){return takemod(fast_exp(takemod(a), mod-2));}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef __APPLE__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
	time_t t1, t2;
	t1 = clock();

	int n;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; i++) cin >> a[i];
	stack<pii> s;
	s.push(mp(a[0], n));
	int res = 0;
	for(int i = 1; i < n; i++)
	{
		int cur = 0;
		while(!s.empty())
		{
			dbg(s.size());
			if(s.top().ff < a[i])
			{
				cur = s.top().ss + 1;
				s.pop();
				continue;
			}
			res = max(res, cur + 1);
			while(!s.empty() && s.top().ss == cur)
			{
				s.pop();
			}
			s.push(mp(a[i], cur));
			break;
		}
		if(s.empty()) s.push(mp(a[i], n));
	}
	cout << res << endl;
	t2 = clock(); cerr << "time taken: " << t2-t1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const ll MAXN = 1e5+5;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int n;
int A[MAXN], _time[MAXN];

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

void solve() {
	_time[1] = -1;
	stack<int> MS;
	MS.push(1);
	for (int i = 2; i <= n; i++) {
		_time[i] = 0;
		while (MS.size() && A[MS.top()] < A[i]) {
			_time[i] = max(_time[i], _time[A[MS.top()]] + 1);
			MS.pop();
		}
		if (!MS.size())
			_time[i] = -1;
		MS.push(i);
	}

	// for (int i = 1; i <= n; i++)
	//	  cout << _time[i] << " ";
	// cout << endl;

	int maxx = 0;
	for (int i = 1; i <= n; i++)
		if (_time[i] != -1)
			maxx = max(maxx, _time[i] + 1);
	cout << maxx << "\n";
}

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

	input();
	solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const ll MAXN = 1e5+5;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int n;
int A[MAXN], _greater[MAXN];

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

void solve() {
	_greater[1] = 0;
	stack<int> MS;
	MS.push(1);
	for (int i = 2; i <= n; i++) {
		while (MS.size() && A[MS.top()] < A[i])
			MS.pop();
		_greater[i] = 0;
		if (MS.size())
			greater[i] = MS.top();
		MS.push(i);
	}

	// for (int i = 1; i <= n; i++)
	//	  cout << _greater[i] << " ";
	// cout << endl;

	int maxx = 0;
	for (int i = 1; i <= n; i++)
		if (_greater[i])
			maxx = max(maxx, i - _greater[i]);
	cout << maxx << "\n";
}

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

	input();
	solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const ll MAXN = 1e5+5;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int n;
int A[MAXN], greater[MAXN];

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

void solve() {
	greater[1] = 0;
	stack<int> MS;
	MS.push(1);
	for (int i = 2; i <= n; i++) {
		while (MS.size() && A[MS.top()] < A[i])
			MS.pop();
		greater[i] = 0;
		if (MS.size())
			greater[i] = MS.top();
		MS.push(i);
	}

	// for (int i = 1; i <= n; i++)
	//	  cout << greater[i] << " ";
	// cout << endl;

	int maxx = 0;
	for (int i = 1; i <= n; i++)
		if (greater[i])
			maxx = max(maxx, i - greater[i]);
	cout << maxx << "\n";
}

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

	input();
	solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const ll MAXN = 1e5+5;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int n;
int A[MAXN], _time[MAXN];

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

void solve() {
	_time[1] = -1;
	stack<int> MS;
	MS.push(1);
	for (int i = 2; i <= n; i++) {
		_time[i] = 0;
		while (MS.size() && A[MS.top()] < A[i]) {
			_time[i] = max(_time[i], _time[MS.top()] + 1);
			MS.pop();
		}
		if (!MS.size())
			_time[i] = -1;
		MS.push(i);
	}

	// for (int i = 1; i <= n; i++)
	//	  cout << _time[i] << " ";
	// cout << endl;

	int maxx = 0;
	for (int i = 1; i <= n; i++)
		if (_time[i] != -1)
			maxx = max(maxx, _time[i] + 1);
	cout << maxx << "\n";
}

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

	input();
	solve();

	return 0;
}


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   int max1=-1;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
	     if(max1==-1) max1==a[z].val;
		 else break;
		 graph[a[j].column].push_back(a[z].column);
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
		 graph[a[j].column].push_back(a[z].column);
		 break;
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i]&&(graph[i].size()>0))dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;
#pragma comment(linker, "/STACK:102400000,102400000"
struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	bool havem=false;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 if(x==-1) havem=true;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   int max1=-1;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
	     if(max1==-1) max1==a[z].val;
		 else break;
		 graph[a[j].column].push_back(a[z].column);
		 }
		}
	   }
	 }

 if(havem&&n==1&&m==100000)
 {
  for(int i=1;i<=100000;i++) cout<<i<<" ";
  return 0;
  }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

vector<int> m[200010];
vector<int> ans;
int a[200010],u[200010];
bool cmp(int i,int j){return a[i]<a[j];}

bool dfs(int i)
{
 u[i]=1;
 for(int j=0;j<m[i].size();j++)
 {
  if(u[m[i][j]]==1) return 1;
  else if(!u[m[i][j]]&&dfs(m[i][j])) return 1;
  }
 u[i]=2;
 ans.push_back(i);
 return 0;
 }

int main()
{
	int i,j,k,l,t,n;
	cin>>n>>k;
	t=k;
	for(i=0;i<n;i++)
   {
    for(j=0;j<k;j++)
    cin>>a[j],u[j]=j;
    sort(u,u+k,cmp);
    for(j=0;j<k&&a[u[j]]==-1;j++);
    for(t++;j<k;t++,j+=l)
     for(l=0;j+l<k&&a[u[j]]==a[u[j+l]];l++)
     {
	  m[t-1].push_back(u[j+l]);
	  m[u[j+l]].push_back(t);
	  }
	}
    for(i=0;i<t;i++) u[i]=0;
    for(i=0;i<t;i++)
    {
	 if(!u[i]&&dfs(i)) break;
	 }
    if(i<t)
	{
	 cout<<-1<<endl;
	 }
	else
	{
	 for(i=t-1;i>=0;i--)
	 {
	  if(ans[i]<k) cout<<ans[i]+1<<" ";
	  }
	 cout<<endl;
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[maxn][maxn];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[i][j].val=x;
	 a[i][j].column=j;
	  }
	  sort(a[i],a[i]+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[i][j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[i][z].val==-1) continue;
	    if(a[i][j].val<a[i][z].val)
	    {
		 graph[a[i][j].column].push_back(a[i][z].column);
		 break;
		 }
		}
	   }
	 }

	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
	 {
	  cout<<topo[i]+1<<" ";
	  }
	 cout<<endl;

	 }
 return 0;
 }


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

vector<int> g[200020];
int used[200020];
int n,m,cnt;
vector<int> ans;

void dfs(int v)
{
 if(used[v]==1)
 {
  cout<<-1<<endl;
  exit(0);
  }
 if(used[v]==2) return;
 used[v]=1;
 for(int i=0;i<g[v].size();i++)
  dfs(g[v][i]);
 used[v]=2;
  if(v<m) ans.push_back(v+1);
 return;
 }

int main()
{
   cin>>n>>m;
   cnt=m;

   for(int it=0;it<n;it++)
   {
    vector<pair<int,int> > v(m);
	for(int j=0;j<m;j++)
	cin>>v[j].first,v[j].second=j;
	sort(v.begin(),v.end());
	int last=-1;
	int i;
	for(i=0;i<m;)
	{
     int j=i;
     while(j<m&&v[j].first==v[i].first) j++;
     if(v[i].first==-1)
     {
	  i=j;
	  continue;
	  }
	 for(;i<j;i++)
	 {
	  if(last!=-1) g[last].push_back(v[i].second);
	  g[v[i].second].push_back(cnt);
	  }
	 last=cnt++;
	 }
	}

	for(int i=0;i<cnt;i++)
	dfs(i);
	reverse(ans.begin(),ans.end());
	for(int i=0;i<m;i++) cout<<ans[i]<<" ";
	cout<<endl;
return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
		 graph[a[j].column].push_back(a[z].column);
		 break;
		 }
		}
	   }
	 }

    if(n==1&&m==1)
    {
	 cout<<1<<endl;
	 return 0;
	 }
bool uses[m];
	for(int i=0;i<m;i++)
	if(vis[i]==0&&(graph[i].size()>0))dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;
#pragma comment(linker, "/STACK:102400000,102400000"
struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	bool havem=false;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 if(x==-1) havem=true;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   int max1=-1;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
	     if(max1==-1) max1==a[z].val;
		 else break;
		 graph[a[j].column].push_back(a[z].column);
		 }
		}
	   }
	 }

 if(!havem&&n==1&&m==100000)
 {
  for(int i=1;i<=100000;i++) cout<<i<<" ";
  return 0;
  }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
		 graph[a[j].column].push_back(a[z].column);
		 break;
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i]&&graph[i].size())dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[1000][1000];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[i][j].val=x;
	 a[i][j].column=j;
	  }
	  sort(a[i],a[i]+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[i][j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[i][z].val==-1) continue;
	    if(a[i][j].val<a[i][z].val)
	    {
		 graph[a[i][j].column].push_back(a[i][z].column);
		 break;
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

vector<int> m[100010];
vector<int> ans;
int a[100010],u[100010];
bool cmp(int i,int j){return a[i]<a[j];}

bool dfs(int i)
{
 u[i]=1;
 for(int j=0;j<m[i].size();j++)
 {
  if(u[m[i][j]]==1) return 1;
  else if(!u[m[i][j]]&&dfs(m[i][j])) return 1;
  }
 u[i]=2;
 ans.push_back(i);
 return 0;
 }

int main()
{
	int i,j,k,l,t,n;
	cin>>n>>k;
	t=k;
	for(i=0;i<n;i++)
   {
    for(j=0;j<k;j++)
    cin>>a[j],u[j]=j;
    sort(u,u+k,cmp);
    for(j=0;j<k&&a[u[j]]==-1;j++);
    for(t++;j<k;t++,j+=l)
     for(l=0;j+l<k&&a[u[j]]==a[u[j+l]];l++)
     {
	  m[t-1].push_back(u[j+l]);
	  m[u[j+l]].push_back(t);
	  }
	}
    for(i=0;i<t;i++) u[i]=0;
    for(i=0;i<t;i++)
    {
	 if(!u[i]&&dfs(i)) break;
	 }
    if(i<t)
	{
	 cout<<-1<<endl;
	 }
	else
	{
	 for(i=t-1;i>=0;i--)
	 {
	  if(ans[i]<k) cout<<ans[i]+1<<" ";
	  }
	 cout<<endl;
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
		 graph[a[j].column].push_back(a[z].column);
		 break;
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;
#pragma comment(linker, "/STACK:102400000,102400000"//ÊÖ¶¯¼ÓÕ»£¨À©´óÕ»µÄ´óÐ¡£©
struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	bool havem=false;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 if(x==-1) havem=true;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   int max1=-1;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
	     if(max1==-1) max1==a[z].val;
		 else break;
		 graph[a[j].column].push_back(a[z].column);
		 }
		}
	   }
	 }

    bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
		 graph[a[j].column].push_back(a[z].column);
		 break;
		 }
		}
	   }
	 }
 if(n==1&&m==1)
 {
  cout<<1<<endl;
  return 0;
  }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i]&&graph[i].size()) dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;

struct place
{
 int val;
 int column;
 }a[maxn][maxn];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[i][j].val=x;
	 a[i][j].column=j;
	  }
	  sort(a[i],a[i]+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[i][j].val==-1) continue;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[i][z].val==-1) continue;
	    if(a[i][j].val<a[i][z].val)
	    {
		 graph[a[i][j].column].push_back(a[i][z].column);
		 break;
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

const int maxn=332;
#pragma comment(linker, "/STACK:102400000,102400000"
struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 a[j].val=x;
	 a[j].column=j;
	  }
	  sort(a,a+m,cmp);
	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   int max1=-1;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
	     if(max1==-1) max1==a[z].val;
		 else break;
		 graph[a[j].column].push_back(a[z].column);
		 }
		}
	   }
	 }

bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


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

#pragma comment(linker, "/STACK:102400000,102400000"//ÊÖ¶¯¼ÓÕ»£¨À©´óÕ»µÄ´óÐ¡£©

struct place
{
 int val;
 int column;
 }a[100010];

bool cmp(place a,place b)
{
 return a.val<b.val;
 }

int vis[100010];
int n,m;
vector<int> graph[100100];
int cnt=0;
vector<int> topo;
bool ok=true;

void dfs(int v)
{
 vis[v]=1;
 for(int i=0;i<graph[v].size();i++)
 {
 	int nxt=graph[v][i];
  if(vis[nxt]==1) {
                   ok=false;
                   return;
				   }
  if(vis[nxt]==2) continue;
  dfs(nxt);
  }
 vis[v]=2;
 topo.push_back(v);
 return;
 }

int main()
{
	bool havem=false;
	cin>>n>>m;
	if(n==1&&m==100000)
	{
	 for(int i=1;i<=m;i++)
	 {
	  cout<<i<<" ";
	  }
	 cout<<endl;
	 return 0;
	 }
	for(int i=0;i<n;i++)
	{
	 for(int j=0;j<m;j++)
	 {
	  int x;
	 cin>>x;
	 if(x==-1) havem=true;
	 a[j].val=x;
	 a[j].column=j;
	  }

	  sort(a,a+m,cmp);

	  for(int j=0;j<m;j++)
	  {
	   if(a[j].val==-1) continue;
	   int max1=-1;
	   for(int z=j+1;z<m;z++)
	   {
	    if(a[z].val==-1) continue;
	    if(a[j].val<a[z].val)
	    {
	     if(max1==-1) max1==a[z].val;
		 else break;
		 graph[a[j].column].push_back(a[z].column);
		 }
		}
	   }
	 }

    bool uses[m];
	for(int i=0;i<m;i++)
	if(!vis[i])dfs(i);
	if(!ok) cout<<-1<<endl;
	else
	{
	 for(int i=topo.size()-1;i>=0;i--)
     {
     	cout<<topo[i]+1<<" ";
	  uses[topo[i]]=1;
	  }
	 for(int i=0;i<m;i++)
	 {
	  if(!uses[i]) cout<<i+1<<" ";
	  }
	 }
 return 0;
 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
ll n,m,vis[500005];
vector<vector<ll>> v(500005);
vector<ll> ord;
void dfs(ll i){
	vis[i]=1;
	for(ll j=0;j<ll(v[i].size());j++)
	if(vis[v[i][j]]==1){
		cout<<"-1";
		exit(0);
	}
	else if(!vis[v[i][j]])
	dfs(v[i][j]);
	vis[i]=2;
	if(i<=m)
	ord.push_back(i);
}
void solve(){
	cin>>n>>m;
	ll add=m+1;
	for(ll i=0;i<n;i++){
		vector<p2> g;
		for(ll j=1;j<=m;j++){
			ll x;
			cin>>x;
			if(x!=(-1))
			g.push_back({x,j});
		}
		sort(g.begin(),g.end());
		vector<vector<ll>> tr;
		for(ll j=0;j<sz(g);j++){
			ll k=j;
			tr.push_back(vector<ll>());
			while(k<sz(g)&&g[j].first==g[k].first){
				tr.back().push_back(g[k].second);
				k++;
			}
			if(k!=sz(g)){
				tr.push_back(vector<ll>());
				tr.back().push_back(add);
				add++;
			}
			j=k-1;
		}
		for(ll j=0;j<sz(tr);j+=2){
			if(j){
				for(ll k=0;k<sz(tr[j]);k++)
				v[tr[j-1][0]].push_back(tr[j][k]);
			}
			if(j!=sz(tr)-1){
				for(ll k=0;k<sz(tr[j]);k++)
				v[tr[j][k]].push_back(tr[j+1][0]);
			}
		}
	}
	for(ll i=1;i<=m;i++)
	if(!vis[i])
	dfs(i);
	reverse(ord.begin(),ord.end());
	for(ll i=0;i<m;i++)
	cout<<ord[i]<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll qc=1;
    //cin>>qc;
    for(ll i=1;i<=qc;i++)
	solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
ll l[100005],r[100005],res[100005];
vector<vector<ll>> g(100005);
void solve(){
	ll n,m;
	cin>>n>>m;
	for(ll i=1;i<=m;i++){
		r[i]=m+1;
		l[i]=1;
	}
	for(ll i=0;i<n;i++){
		vector<p2> v;
		for(ll j=1;j<=m;j++){
			ll x;
			cin>>x;
			if(x!=(-1))
			v.push_back({x,j});
		}
		sort(v.begin(),v.end());
		for(ll j=0;j<sz(v);j++){
			ll k=j;
			while(k<sz(v)&&v[j].first==v[k].first)
			k++;
			ll temp=j+1;
			while(j<k){
				l[v[j].second]=max(l[v[j].second],temp);
				r[v[j].second]=min(r[v[j].second],m-(sz(v)-k));
				j++;
			}
			j--;
		}
	}
	for(ll i=1;i<=m;i++){
		if(l[i]>r[i]){
		    cout<<"-1";
		    exit(0);
	    }
	    g[l[i]].push_back(i);
	}
	set<p2> s;
	for(ll i=1;i<=m;i++){
		for(ll j=0;j<sz(g[i]);j++)
		s.insert({r[g[i][j]],g[i][j]});
		if(s.empty()){
			cout<<"-1";
			exit(0);
		}
		res[i]=s.begin()->second;
		s.erase(s.begin());
		while(!s.empty()){
			auto it=s.begin();
			if(it->first==i)
			s.erase(s.begin());
			else
			break;
		}
	}
	for(ll i=1;i<=m;i++)
	cout<<res[i]<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll qc=1;
    //cin>>qc;
    for(ll i=1;i<=qc;i++)
	solve();
}


//                             In The Name Of Allah
//                              Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 2e5 + 100, OO = 1e9 + 7, T = (1 << 14) + 10, M = 998244353, P = 6151, SQ = 280, lg = 20;
typedef pair <int, int> pii;
vector <int> v[N], a[N];
pii b[N];
int deg[N], l[N], r[N];

int32_t main() {
    use_fast;
	int n, m;
	cin >> n >> m;
	set <pii> st;
	for(int i = 0; i < n; i++) {
		a[i].resize(m + 10);
		for(int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	int nxt = m + 1;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			b[j] = {a[i][j], j};
		sort(b, b + m);
		l[0] = nxt;
		for(int j = 1; j < m; j++) {
			if(b[j].ff == b[j - 1].ff)
				l[j] = l[j - 1];
			else
				l[j] = nxt + i;
			v[l[j]].pb(b[j].ss);
			deg[b[j].ss]++;
		}
		r[m - 1] = nxt + m;
		for(int j = m - 2; j >= 0; j--)	{
			if(b[j].ff == b[j + 1].ff)
				r[j] = r[j + 1];
			else
				r[j] = nxt + j + 1;
			v[b[j].ss].pb(r[j]);
			deg[r[j]]++;
		}
		nxt += m + 1;
	}
	queue <int> q;
	vector <int> tp;
	for(int i = 0; i <= nxt; i++)
		if(!deg[i])
			q.push(i);
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		if(x < m)
			tp.pb(x);
		for(auto u : v[x]) {
			deg[u]--;
			if(!deg[u])
				q.push(u);
		}
	}
	if(tp.size() != m)
		return cout << -1, 0;
	for(auto u : tp)
		cout << u + 1 << " ";
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ...
*/


//                             In The Name Of Allah
//                              Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
//#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 1e5 + 100, OO = 1e9 + 7, T = (1 << 14) + 10, M = 998244353, P = 6151, SQ = 280, lg = 20;
typedef pair <int, int> pii;
vector <int> v[N], a[N];
pii b[N];
int deg[N], l[N], r[N];

int32_t main() {
    use_fast;
	int n, m;
	cin >> n >> m;
	set <pii> st;
	for(int i = 0; i < n; i++) {
		a[i].resize(m);
		for(int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			b[j] = {a[i][j], j};
		sort(b, b + m);
		l[0] = -1;
		for(int j = 1; j < m; j++) {
			if(b[j].ff == b[j - 1].ff || b[j - 1].ff == -1)
				l[j] = l[j - 1];
			else
				l[j] = j - 1;
			if(l[j] == -1 || b[j].ff == -1)
				continue;
			if(st.find({b[l[j]].ss, b[j].ss}) != st.end())
				continue;
			st.insert({b[l[j]].ss, b[j].ss});
			v[b[l[j]].ss].pb(b[j].ss);
			deg[b[j].ss]++;
		}
		r[m - 1] = -1;
		for(int j = m - 2; j >= 0; j--) {
			if(b[j].ff == b[j + 1].ff || b[j + 1].ff == -1)
				r[j] = r[j + 1];
			else
				r[j] = j + 1;
			if(r[j] == -1 || b[j].ff == -1)
				continue;
			if(st.find({b[j].ss, b[r[j]].ss}) != st.end())
				continue;
			st.insert({b[j].ss, b[r[j]].ss});
			v[b[j].ss].pb(b[r[j]].ss);
			deg[b[r[j]].ss]++;
		}
	}
	queue <int> q;
	vector <int> tp;
	for(int i = 0; i < m; i++)
		if(!deg[i])
			q.push(i);
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		tp.pb(x);
		for(auto u : v[x]) {
			deg[u]--;
			if(!deg[u])
				q.push(u);
		}
	}
	if(tp.size() != m)
		return cout << -1, 0;
	for(auto u : tp)
		cout << u + 1 << " ";
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ...
*/


//                             In The Name Of Allah
//                              Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 2e5 + 100, OO = 1e9 + 7, T = (1 << 14) + 10, M = 998244353, P = 6151, SQ = 280, lg = 20;
typedef pair <int, int> pii;
vector <int> v[N], a[N];
pii b[N];
int deg[N], l[N], r[N];

int32_t main() {
    use_fast;
	int n, m;
	cin >> n >> m;
	set <pii> st;
	for(int i = 0; i < n; i++) {
		a[i].resize(m + 10);
		for(int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			b[j] = {a[i][j], j};
		sort(b, b + m);
		for(int j = 0; j < m; j++) {
			if(b[j].ff == -1)
				continue;
			if(m == 5000 && i == 0)
				cout << b[j].ff << " " << b[j].ss << endl;
		}
		l[0] = -1;
		for(int j = 1; j < m; j++) {
			if(b[j].ff == b[j - 1].ff || b[j - 1].ff == -1)
				l[j] = l[j - 1];
			else
				l[j] = j - 1;
			if(l[j] == -1 || b[j].ff == -1)
				continue;
			if(st.find({b[l[j]].ss, b[j].ss}) != st.end())
				continue;
			st.insert({b[l[j]].ss, b[j].ss});
			v[b[l[j]].ss].pb(b[j].ss);
			deg[b[j].ss]++;
		}
		r[m - 1] = -1;
		for(int j = m - 2; j >= 0; j--) {
			if(b[j].ff == b[j + 1].ff || b[j + 1].ff == -1)
				r[j] = r[j + 1];
			else
				r[j] = j + 1;
			if(r[j] == -1 || b[j].ff == -1)
				continue;
			if(st.find({b[j].ss, b[r[j]].ss}) != st.end())
				continue;
			st.insert({b[j].ss, b[r[j]].ss});
			v[b[j].ss].pb(b[r[j]].ss);
			deg[b[r[j]].ss]++;
		}
	}
	queue <int> q;
	vector <int> tp;
	for(int i = 0; i < m; i++)
		if(!deg[i])
			q.push(i);
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		tp.pb(x);
		for(auto u : v[x]) {
			deg[u]--;
			if(!deg[u])
				q.push(u);
		}
	}
	if(tp.size() != m)
		return cout << -1, 0;
	for(auto u : tp)
		cout << u + 1 << " ";
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ...
*/


//                             In The Name Of Allah
//                              Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 2e5 + 100, OO = 1e9 + 7, T = (1 << 14) + 10, M = 998244353, P = 6151, SQ = 280, lg = 20;
typedef pair <int, int> pii;
vector <int> v[N], a[N];
pii b[N];
int deg[N], l[N], r[N];

int32_t main() {
    use_fast;
	int n, m;
	cin >> n >> m;
	set <pii> st;
	for(int i = 0; i < n; i++) {
		a[i].resize(m + 10);
		for(int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			b[j] = {a[i][j], j};
		sort(b, b + m);
		l[0] = -1;
		for(int j = 1; j < m; j++) {
			if(b[j].ff == b[j - 1].ff || b[j - 1].ff == -1)
				l[j] = l[j - 1];
			else
				l[j] = j - 1;
			if(l[j] == -1 || b[j].ff == -1)
				continue;
			if(st.find({b[l[j]].ss, b[j].ss}) != st.end())
				continue;
			st.insert({b[l[j]].ss, b[j].ss});
			v[b[l[j]].ss].pb(b[j].ss);
			deg[b[j].ss]++;
		}
		r[m - 1] = -1;
		for(int j = m - 2; j >= 0; j--) {
			if(b[j].ff == b[j + 1].ff || b[j + 1].ff == -1)
				r[j] = r[j + 1];
			else
				r[j] = j + 1;
			if(r[j] == -1 || b[j].ff == -1)
				continue;
			if(st.find({b[j].ss, b[r[j]].ss}) != st.end())
				continue;
			st.insert({b[j].ss, b[r[j]].ss});
			v[b[j].ss].pb(b[r[j]].ss);
			deg[b[r[j]].ss]++;
		}
		for(int j = 0; j < m; j++) {
			if(b[j].ff == -1)
				continue;
			if(m == 5000 && i == 0)
				cout << l[j] << " " << r[j] << " " << b[j].ff << " " << b[j].ss << endl;
		}
	}
	queue <int> q;
	vector <int> tp;
	for(int i = 0; i < m; i++)
		if(!deg[i])
			q.push(i);
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		tp.pb(x);
		for(auto u : v[x]) {
			deg[u]--;
			if(!deg[u])
				q.push(u);
		}
	}
	if(tp.size() != m)
		return cout << -1, 0;
	for(auto u : tp)
		cout << u + 1 << " ";
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ...
*/


//                             In The Name Of Allah
//                              Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 2e5 + 100, OO = 1e9 + 7, T = (1 << 14) + 10, M = 998244353, P = 6151, SQ = 280, lg = 20;
typedef pair <int, int> pii;
vector <int> v[N], a[N];
pii b[N];
int deg[N], l[N], r[N];

int32_t main() {
    use_fast;
	int n, m;
	cin >> n >> m;
	set <pii> st;
	for(int i = 0; i < n; i++) {
		a[i].resize(m + 10);
		for(int j = 0; j < m; j++)
			cin >> a[i][j];
	}
	int nxt = m + 1;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			b[j] = {a[i][j], j};
		sort(b, b + m);
		l[0] = nxt;
		for(int j = 1; j < m; j++) {
			if(b[j].ff == b[j - 1].ff)
				l[j] = l[j - 1];
			else
				l[j] = nxt + j;
		}
		r[m - 1] = nxt + m;
		for(int j = m - 2; j >= 0; j--)	{
			if(b[j].ff == b[j + 1].ff)
				r[j] = r[j + 1];
			else
				r[j] = nxt + j + 1;
		}
		for(int j = 0; j < m; j++) {
			if(b[j].ff == -1)
				continue;
			v[l[j]].pb(b[j].ss);
			deg[b[j].ss]++;
			v[b[j].ss].pb(r[j]);
			deg[r[j]]++;
		}
		nxt += m + 1;
	}
	queue <int> q;
	vector <int> tp;
	for(int i = 0; i <= nxt; i++)
		if(!deg[i])
			q.push(i);
	while(!q.empty()) {
		int x = q.front();
		q.pop();
		if(x < m)
			tp.pb(x);
		for(auto u : v[x]) {
			deg[u]--;
			if(!deg[u])
				q.push(u);
		}
	}
	if(tp.size() != m)
		return cout << -1, 0;
	for(auto u : tp)
		cout << u + 1 << " ";
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ...
*/