#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return  query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,arr[i].x-1)==arr[i].x)
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
	int l,r;
	long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
	segtree[i].l=l;
	segtree[i].r=r;
	if(l==r)
	{
		segtree[i].nsum=num[l];
		return;
	}
	int mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
	if(segtree[i].l==a&&segtree[i].r==b)
	{
		return segtree[i].nsum;
	}
	int mid=(segtree[i].l+segtree[i].r)>>1;
	if(b<=mid)
		return query(i<<1,a,b);
	else if(a>mid)
		return query(i<<1|1,a,b);
	else
		return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
	int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
	if(n1.x!=n2.x)
		return n1.x<n2.x;
	else
		return n1.id<n2.id;
}

int main()
{
	int n,i,ans,now;
	while(cin>>n)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			arr[i].id=i;
			arr[i].x=num[i];
		}
		build(1,1,n);
		sort(arr+1,arr+1+n,cmp);
		ans=1;
		now=arr[i].id;
		for(i=2;i<=n;i++)
		{
			if(arr[i].id>now)
			{
				if(query(1,1,now)<=query(1,now+1,arr[i].id))
					ans++;
				now=arr[i].id;
			}
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(i==1)
            {
                now=arr[i].id;
                ans++;
            }
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(i==1)
            {
                now=arr[i].id;
                ans++;
                continue;
            }
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300],segtree2[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    segtree2[i].l=l;
    segtree2[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        segtree2[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
    segtree2[i].nsum=min(segtree2[i<<1].nsum,segtree2[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

long long query2(int i,int a,int b)
{
    if(segtree2[i].l==a&&segtree2[i].r==b)
    {
        return segtree2[i].nsum;
    }
    int mid=(segtree2[i].l+segtree2[i].r)>>1;
    if(b<=mid)
        return query2(i<<1,a,b);
    else if(a>mid)
        return query2(i<<1|1,a,b);
    else
        return min(query2(i<<1,a,mid),query2(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=1;
        now=arr[1].id;
        for(i=2;i<=n;i++)
        {
            if(query(1,1,i-1)<=query2(1,i,n))
                ans++;
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
	int l,r;
	long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
	segtree[i].l=l;
	segtree[i].r=r;
	if(l==r)
	{
		segtree[i].nsum=num[l];
		return;
	}
	int mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
	if(segtree[i].l==a&&segtree[i].r==b)
	{
		return segtree[i].nsum;
	}
	int mid=(segtree[i].l+segtree[i].r)>>1;
	if(b<=mid)
		return query(i<<1,a,b);
	else if(a>mid)
		return query(i<<1|1,a,b);
	else
		return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
	int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
	if(n1.x!=n2.x)
		return n1.x<n2.x;
	else
		return n1.id<n2.id;
}

int main()
{
	int n,i,ans,now;
	while(cin>>n)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			arr[i].id=i;
			arr[i].x=num[i];
		}
		build(1,1,n);
		sort(arr+1,arr+1+n,cmp);
		ans=1;
		now=arr[1].id;
		for(i=2;i<=n;i++)
		{
			if(arr[i].id>now)
			{
				if(query(1,1,now)<=query(1,now+1,arr[i].id))
					ans++;
				now=arr[i].id;
			}
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>using namespace std;int num[100100];struct node{ int l,r;    long long nsum;}segtree[300300];void build(int i,int l,int r){  segtree[i].l=l; segtree[i].r=r; if(l==r)    {       segtree[i].nsum=num[l];     return; }   int mid=(l+r)>>1;   build(i<<1,l,mid);  build(i<<1|1,mid+1,r);  segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);}long long query(int i,int a,int b){   if(segtree[i].l==a&&segtree[i].r==b)    {       return segtree[i].nsum; }   int mid=(segtree[i].l+segtree[i].r)>>1; if(b<=mid)      return query(i<<1,a,b); else if(a>mid)      return query(i<<1|1,a,b);   else        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));}struct my{ int id,x;}arr[100100];bool cmp(my n1,my n2){    if(n1.x!=n2.x)      return n1.x<n2.x;   else        return n1.id<n2.id;}int main(){ int n,i,ans,now;    while(cin>>n)   {       for(i=1;i<=n;i++)       {           scanf("%d",&num[i]);            arr[i].id=i;            arr[i].x=num[i];        }       build(1,1,n);       sort(arr+1,arr+1+n,cmp);        ans=1;      now=arr[i].id;      for(i=2;i<=n;i++)       {           if(arr[i].id>now)           {               if(query(1,1,now)<=query(1,now+1,arr[i].id))                    ans++;              now=arr[i].id;          }       }       cout<<ans<<endl;    }}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if((arr[i].id==now+1&&query(1,1,now)==arr[i].x)||(arr[i].id>now+1&&query(1,1,now)<=query(1,now+1,arr[i].id)))
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)<=query(1,1,arr[i].id))
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return  query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(arr[i].x!=arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,now)==arr[i].x)
                        ans++;
                    now=arr[i].id;
                }

            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int num[100100];
struct node
{
    int l,r;
    long long nsum;
}segtree[300300];

void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    if(l==r)
    {
        segtree[i].nsum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segtree[i].nsum=max(segtree[i<<1].nsum,segtree[i<<1|1].nsum);
}

long long query(int i,int a,int b)
{
    if(segtree[i].l==a&&segtree[i].r==b)
    {
        return segtree[i].nsum;
    }
    int mid=(segtree[i].l+segtree[i].r)>>1;
    if(b<=mid)
        return  query(i<<1,a,b);
    else if(a>mid)
        return query(i<<1|1,a,b);
    else
        return max(query(i<<1,a,mid),query(i<<1|1,mid+1,b));
}

struct my
{
    int id,x;
}arr[100100];

bool cmp(my n1,my n2)
{
    if(n1.x!=n2.x)
        return n1.x<n2.x;
    else
        return n1.id<n2.id;
}

int main()
{
    int n,i,ans,now;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            arr[i].id=i;
            arr[i].x=num[i];
        }
        build(1,1,n);
        sort(arr+1,arr+1+n,cmp);
        ans=0;
        arr[0].x=0;arr[0].id=0;now=0;
        for(i=1;i<=n;i++)
        {
            if(arr[i].x>arr[i-1].x)
            {
                if(arr[i].id>now)
                {
                    ans++;
                    now=arr[i].id;
                }
            }
            else
            {
                if(arr[i].id>now)
                {
                    if(query(1,1,arr[i].id-1)==arr[i].x)
                        ans++;
                    now=arr[i].id;
                }
            }
        }
        cout<<ans<<endl;
    }
}


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

const int maxn = 1e5 + 10;
int n,h[maxn],t[maxn];
int l[maxn],r[maxn];
int d[maxn];

int main()
{
	//freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",h+i),t[i] = h[i];
	sort(t,t+n);
	for(int i=0;i<n;++i)
	{
		r[i] = max(i,int(lower_bound(t,t+n,h[i])-t));
		l[i] = min(i,int(upper_bound(t,t+n,h[i])-t)-1);
	}
	memset(d,0,sizeof d);
	for(int i=0;i<n;++i)
	{
		if(l[i])
			d[r[i]] = max(d[r[i]],d[l[i]-1] + 1);
		else
			d[r[i]] = max(d[r[i]],1);
	}
	printf("%d\n",d[n-1]);
	return 0;
}


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

const int maxn = 1e5 + 10;
int n,h[maxn],t[maxn];
map<int,int> mp;

int main()
{
	//freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",h+i),t[i] = h[i];
	sort(t,t+n);
	mp.clear();
	int cur = 0,ans = 0;
	for(int i=0;i<n;++i)
	{
		int p = int(lower_bound(t,t+n,h[i])-t) + mp[h[i]];
		++mp[h[i]];
		if(p > cur)
		{
			cur = p;
		}
		if(cur == i) ++ans;
	}
	printf("%d\n",ans);
	return 0;
}


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

const int maxn = 1e5 + 10;
int n,h[maxn],t[maxn];
int l[maxn],r[maxn];
int d[maxn];

int main()
{
	//freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",h+i),t[i] = h[i];
	sort(t,t+n);
	for(int i=0;i<n;++i)
	{
		r[i] = max(i,int(lower_bound(t,t+n,h[i])-t));
		l[i] = min(i,int(upper_bound(t,t+n,h[i])-t)-1);
		//printf("%d %d\n",l[i],r[i]);
	}
	for(int i=0;i<n;++i)
	{
		r[l[i]] = max(r[l[i]],r[i]);
	}
	int i = 0,cnt = 0;
	while(1)
	{
		int cur = r[i];
		for(int j=l[i];j<=r[i];++j)
		{
			if(r[j] > cur) cur = r[j];
		}
		i = cur+1;
		++cnt;
		if(i >= n) break;
	}
	printf("%d\n",cnt);
	return 0;
}


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

const int maxn = 1e5 + 10;
int n,h[maxn],t[maxn];
int l[maxn],r[maxn];

int main()
{
	//freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",h+i),t[i] = h[i];
	sort(t,t+n);
	memset(r,0,sizeof r);
	memset(l,0,sizeof l);
	for(int i=0;i<n;++i)
	{
		r[i] = max(i,int(lower_bound(t,t+n,h[i])-t));
		l[i] = min(i,int(upper_bound(t,t+n,h[i])-t)-1);
		if(l[i] < i && r[i] > i) l[i] = r[i] = i;
		//printf("%d %d\n",l[i],r[i]);
	}
	for(int i=0;i<n;++i)
	{
		r[l[i]] = max(r[l[i]],r[i]);
	}
	int i = 0,cnt = 0;
	while(1)
	{
		int cur = r[i];
		for(int j=l[i];j<=r[i];++j)
		{
			if(r[j] > cur) cur = r[j];
		}
		i = cur+1;
		++cnt;
		if(i >= n) break;
	}
	printf("%d\n",max(1,cnt));
	return 0;
}


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

const int maxn = 1e5 + 10;
int n,h[maxn],t[maxn];
int l[maxn],r[maxn];

int main()
{
	//freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",h+i),t[i] = h[i];
	sort(t,t+n);
	memset(r,0,sizeof r);
	memset(l,0,sizeof l);
	for(int i=0;i<n;++i)
	{
		r[i] = max(i,int(lower_bound(t,t+n,h[i])-t));
		l[i] = min(i,int(upper_bound(t,t+n,h[i])-t)-1);
		if(l[i] != i && r[i] != i) l[i] = r[i] = i;
	//	printf("%d %d\n",l[i],r[i]);
	}
	for(int i=0;i<n;++i)
	{
		r[l[i]] = max(r[l[i]],r[i]);
	}
	int i = 0,cnt = 0;
	while(1)
	{
		int cur = r[i];
		for(int j=l[i];j<=r[i];++j)
		{
			if(r[j] > cur) cur = r[j];
		}
		i = cur+1;
		++cnt;
		if(i >= n) break;
	}
	printf("%d\n",max(1,cnt));
	return 0;
}


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

const int maxn = 1e5 + 10;
int n,h[maxn],t[maxn];
int l[maxn],r[maxn];
int d[maxn];
vector<int> v[maxn];

int main()
{
	//freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",h+i),t[i] = h[i];
	sort(t,t+n);
	for(int i=0;i<n;++i)
	{
		r[i] = max(i,int(lower_bound(t,t+n,h[i])-t));
		l[i] = min(i,int(upper_bound(t,t+n,h[i])-t)-1);
		v[l[i]].push_back(r[i]);
	}
	for(int i=0;i<n;++i)
	{
		sort(v[i].begin(),v[i].end(),greater<int>());
	}
	int cnt = 0,p = 0;
	while(1)
	{
		if(p >= n) break;
		++cnt;
		p = v[p][0]+1;
	}
	printf("%d\n",cnt);
	return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

long long arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=0;
    for(i=0;i<n;i++)
        cin >> arr[i];
    long long mini=1<<60,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<60;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        /*if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {*/
            if(cm<=minii[i])//&&arr[i]>=cm)
            {
                ans++;
                cm=max(cm,maxi);
            }
            cm=max(cm,arr[i]);
        //}
    }
    cout << (ans?ans:1) << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

long long arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=0;
    for(i=0;i<n;i++)
        cin >> arr[i];
    long long mini=1<<60,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<60;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        /*if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {*/
            if(arr[i]<=minii[i]&&arr[i]>=cm)
            {
                ans++;
                cm=max(cm,maxi);
            }
            maxi=max(maxi,arr[i]);
        //}
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

int arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=0;
    for(i=0;i<n;i++)
        cin >> arr[i];
    int mini=1<<30,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<30;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        /*if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {*/
            if(arr[i]<=minii[i]&&arr[i]>=cm)
            {
                ans++;
                cm=maxi;
            }
            maxi=max(maxi,arr[i]);
        //}
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

long long arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=0;
    for(i=0;i<n;i++)
        cin >> arr[i];
    long long mini=1<<60,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<60;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        /*if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {*/
            if(arr[i]<=minii[i]&&arr[i]>=cm)
            {
                ans++;
                cm=max(cm,maxi);
            }
            maxi=max(maxi,arr[i]);
        //}
    }
    cout << (ans?ans:1) << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

long long arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=0;
    for(i=0;i<n;i++)
        cin >> arr[i];
    long long mini=1<<60,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<60;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        /*if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {*/
            if(arr[i]<=minii[i]&&arr[i]>=cm)
            {
                ans++;
                cm=maxi;
            }
            maxi=max(maxi,arr[i]);
        //}
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

int arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=0;
    for(i=0;i<n;i++)
        cin >> arr[i];
    int mini=1<<30,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<30;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        /*if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {*/
            if(arr[i]<=minii[i])
            {
                ans++;
            }
        //}
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

int arr[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=1;
    for(i=0;i<n;i++)
        cin >> arr[i];
    for(i=1;i<n;i++)
    {
        if(arr[i]>arr[i-1]) ans++;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back

using namespace std;

int arr[100005],minii[100005];
int n;

int main()
{
    cin >> n;
    int i;
    int ans=1;
    for(i=0;i<n;i++)
        cin >> arr[i];
    int mini=1<<30,maxi=-1<<30,cm=-1<<30;
    for(i=0;i<n;i++)
        mini=min(mini,arr[i]);
    minii[n-1]=arr[n-1];
    minii[n]=1<<30;
    for(i=n-2;i>=0;i--)
        minii[i]=min(minii[i+1],arr[i]);
    int fp=1;
    for(i=0;i<n;i++)
    {
        if(fp)
        {
            if(arr[i]==mini) fp=0;
            maxi=max(maxi,arr[i]);
        }
        else
        {
            if(arr[i]>=maxi&&arr[i]<=minii[i+1])
            {
                ans++;
                maxi=cm;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = l; mid <= min(l + 800, r); mid++) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
		for (int mid = max(r - 800, l); mid <= r; mid++) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}

	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = l; mid <= min(l + 2000, r); mid++) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
		for (int mid = max(r - 2000, l); mid <= r; mid++) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}

	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9+5;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int n;
int dp[N];

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

	int mx = -1;
	for (int i = 0; i < n; i++) {
		mx = max(mx, a[i]);
	if (i)
		dp[i] = max(dp[i], dp[i - 1]);

		int l = i + 1;
		int r = n - 1;

		int mn = INF;

		for (int j = i + 1; j < n; j++) {
			mn = min(mn, a[j]);
			if (mn >= mx)
				dp[j] = max(dp[j], dp[i] + 1);
		}
	}

	printf("%d", dp[n - 1]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e8 + 5;
typedef long long li;
li X;
#define mp make_pair
int IT = 0;

li calc(li x, li y) {
	li ans = 0;
	for (int k = 1; k <= y && k <= x && k <= N; k++) {
		IT++;if (IT > 3e8)
			break;

		ans += (x - k + 1) * (y - k + 1);
		if (ans > X) break;
	}
	return ans;
}

int main() {
	scanf("%I64d", &X);

	vector<pair<li, li>> ans;

	for (int i = 1; i <= 100000000; i++) {

		if (IT > 3e8)
			break;

		li l = i;
		li r = X;

		while (r - l > 2) {
			li mid = (l + r) / 2;
			if (calc(i, mid) > X) {
				r = mid;
			}
			else {
				l = mid;
			}
		}
		for (li mid = l; mid <= r; mid++) {
			if (calc(i, mid) == X) {
				ans.push_back(mp(i, mid));
				if (i != mid)
					ans.push_back(mp(mid, i));
				break;
			}
		}
	}
	sort(ans.begin(), ans.end());

	printf("%d\n", (int)ans.size());
	for (int i = 0; i < ans.size(); i++)
		printf("%I64d %I64d\n", ans[i].first, ans[i].second);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9+5;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int n;
int dp[N];

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

	int mx = -1;
	for (int i = 0; i < n; i++) {
		mx = max(mx, a[i]);

		int l = i + 1;
		int r = n - 1;

		int mn = INF;

		for (int j = i + 1; j < n; j++) {
			mn = min(mn, a[j]);
			if (mn >= mx)
				dp[j] = max(dp[j], dp[i] + 1);
		}
	}

	printf("%d", dp[n - 1]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = r; mid >= l; mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
				break;
			}
		}

	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int calc(int r) {
	if (r == 0)
		return dp[r] = 0;
	for (int mid = r; mid >= 1; mid--) {
		if (getMn(mid, r) >= mx[mid - 1]) {
			return dp[r] = calc(mid - 1) + 1;
		}
	}
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	printf("%d", calc(n));
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = r; mid >= l; mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], qp[mid - 1] + 1);
				break;
			}
		}
		qp[i] = max(qp[i], dp[i]);
		qp[i] = max(qp[i], qp[i - 1]);
	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = r; mid >= l; mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = l; mid <= min(l + 1000, r); mid++) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
		for (int mid = max(r - 1000, l); mid <= r; mid++) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}

	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = r; mid >= max(r - 5000, l); mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
		for (int mid = min(r, l + 5000); mid >= l; mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		while (r - l > 2) {
			int mid = (l + r) / 2;
			if (getMn(mid, i) >= mx[mid - 1]) {
				l = mid;
			}
			else {
				r = mid;
			}
		}

		for (int mid = r; mid >= l; mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], qp[mid - 1] + 1);
				qp[i] = max(qp[i], dp[i]);
				qp[i] = max(qp[i], qp[i - 1]);
				break;
			}
		}

	}

	printf("%d", dp[n]);
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int calc(int r) {
	if (r == 0)
		return 0;
	for (int mid = r; mid >= 1; mid--) {
		if (getMn(mid, r) >= mx[mid - 1]) {
			return calc(mid - 1) + 1;
		}
	}
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	printf("%d", calc(n));
}


#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>

using namespace std;

const int INF = 1e9;
const int N = 1e5 + 5;
const int K = 25;
int a[N];
int b[N][K];
int n;
int lg[N];
int dp[N];
int qp[N];
int mx[N];

void hard() {
	int j = 0;

	for (int i = 1; i < N; i++) {
		if ((1 << (j + 1)) == i) {
			j++;
		}
		lg[i] = j;
	}
	for (int i = 1; i <= n; i++) {
		b[i][0] = a[i];
	}
	for (int k = 1; k < K; k++) {
		for (int i = 1; i <= n; i++) {
			int ni = i + (1 << (k - 1));
			b[i][k] = b[i][k - 1];
			if (ni <= n)
				b[i][k] = min(b[i][k], b[ni][k - 1]);
		}
	}

}

inline int getMn(int l, int r) {
	int len = lg[r - l + 1];

	return min(b[l][len], b[r - (1 << len) + 1][len]);
}

int main() {
	scanf("%d", &n);
	mx[0] = -INF;

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

	hard();

	dp[0] = 0;

	for (int i = 1; i <= n; i++) {
		int l = 1;
		int r = i;

		for (int mid = r; mid >= max(r - 500, l); mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
		for (int mid = min(r, l + 500); mid >= l; mid--) {
			if (getMn(mid, i) >= mx[mid - 1]) {
				dp[i] = max(dp[i], dp[mid - 1] + 1);
			}
		}
	}

	printf("%d", dp[n]);
}


#include<bits/stdc++.h>

#define lli long long int
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);

const double EPS = 1e-4;
const lli MOD = 1000000007ll;
const lli HASHMOD[2]={1000000009ll,1100000009ll};
const double PI = 3.14159265359;
int INF = 2147483645;
lli INFINF = 9223372036854775807;
int debug = 0;

template <class T>T Max2(T a,T b){return a<b?b:a;}
template <class T>T Min2(T a,T b){return a<b?a:b;}
template <class T>T Max3(T a,T b,T c){return Max2(Max2(a,b),c);}
template <class T>T Min3(T a,T b,T c){return Min2(Min2(a,b),c);}
template <class T>T Max4(T a,T b,T c,T d){return Max2(Max2(a,b),Max2(c,d));}
template <class T>T Min4(T a,T b,T c,T d){return Min2(Min2(a,b),Min2(c,d));}

using namespace std;

lli bit_count(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
lli bit(lli _mask,lli _i){return (_mask&(1<<_i))==0?0:1;}
lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b){lli x=a+b;if(x>MOD)x-=MOD;return x;}
lli sub(lli a,lli b){lli x=a-b;if(x<0)x+=MOD;return x;}
lli mul(lli a,lli b){lli x=a*b;x%=MOD;return x;}

lli n;
lli h[100010];
vector<pair<lli,lli> >v;
bool vis[100010];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    debug = 1;
#endif
    srand (time(NULL));

    slli(n);
    for(lli i=1;i<=n;i++)
        slli(h[i]);

    for(lli i=1;i<=n;i++)
        v.pb({h[i],i});

    sort(all(v));

    lli cur=1;
    lli ans=0;
    for(int i=0;i<n;)
    {
        vis[v[i].S]=true;
        i++;
        while(v[i].F==v[i-1].F)
        {
            vis[v[i].S]=true;
            i++;
        }
        if(vis[cur])
        {
            bool allsame=true;
            lli x=h[cur];
            cur++;
            lli sz=1;
            while(vis[cur] && cur<=n)
            {
                if(h[cur]!=x)
                    allsame=false;
                sz++;
                cur++;
            }
            if(allsame)
                ans+=sz;
            else
                ans++;
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);

const double EPS = 1e-4;
const lli MOD = 1000000007ll;
const lli HASHMOD[2]={1000000009ll,1100000009ll};
const double PI = 3.14159265359;
int INF = 2147483645;
lli INFINF = 9223372036854775807;
int debug = 0;

template <class T>T Max2(T a,T b){return a<b?b:a;}
template <class T>T Min2(T a,T b){return a<b?a:b;}
template <class T>T Max3(T a,T b,T c){return Max2(Max2(a,b),c);}
template <class T>T Min3(T a,T b,T c){return Min2(Min2(a,b),c);}
template <class T>T Max4(T a,T b,T c,T d){return Max2(Max2(a,b),Max2(c,d));}
template <class T>T Min4(T a,T b,T c,T d){return Min2(Min2(a,b),Min2(c,d));}

using namespace std;

lli bit_count(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
lli bit(lli _mask,lli _i){return (_mask&(1<<_i))==0?0:1;}
lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b){lli x=a+b;if(x>MOD)x-=MOD;return x;}
lli sub(lli a,lli b){lli x=a-b;if(x<0)x+=MOD;return x;}
lli mul(lli a,lli b){lli x=a*b;x%=MOD;return x;}

int n;
int h[100010];
set<int> Set;set<int>::iterator it;
map<int,int> Map;
int cnt1[100010];
int p[100010];
int last[100010];
int cnt2[100010];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    debug = 1;
#endif
    srand (time(NULL));

    si(n);
    for(int i=0;i<n;i++)
    {
        si(h[i]);
        Set.insert(h[i]);
    }
    int tmp=0;
    for(it=Set.begin();it!=Set.end();it++)
    {
        tmp++;
        Map[(*it)]=tmp;
    }
    for(int i=0;i<n;i++)
        h[i]=Map[h[i]];
    for(int i=0;i<n;i++)
        cnt1[h[i]]++;

    for(int i=1;i<=tmp;i++)
    {
        p[i]=p[i-1]+cnt1[i];
        for(int j=p[i-1];j<p[i];j++)
            last[j]=i;
    }
    int ans=0;
    int m=-1;

    for(int i=0;i<n;i++)
    {
        cnt2[h[i]]++;
        m=Max2(m,h[i]);
        if(m>last[i])
            continue;
        else if(m==last[i])
        {
            int temp=i+1-cnt2[last[i]];
            if(temp==p[last[i] - 1])
                ans++;
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);

const double EPS = 1e-4;
const lli MOD = 1000000007ll;
const lli HASHMOD[2]={1000000009ll,1100000009ll};
const double PI = 3.14159265359;
int INF = 2147483645;
lli INFINF = 9223372036854775807;
int debug = 0;

template <class T>T Max2(T a,T b){return a<b?b:a;}
template <class T>T Min2(T a,T b){return a<b?a:b;}
template <class T>T Max3(T a,T b,T c){return Max2(Max2(a,b),c);}
template <class T>T Min3(T a,T b,T c){return Min2(Min2(a,b),c);}
template <class T>T Max4(T a,T b,T c,T d){return Max2(Max2(a,b),Max2(c,d));}
template <class T>T Min4(T a,T b,T c,T d){return Min2(Min2(a,b),Min2(c,d));}

using namespace std;

lli bit_count(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
lli bit(lli _mask,lli _i){return (_mask&(1<<_i))==0?0:1;}
lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b){lli x=a+b;if(x>MOD)x-=MOD;return x;}
lli sub(lli a,lli b){lli x=a-b;if(x<0)x+=MOD;return x;}
lli mul(lli a,lli b){lli x=a*b;x%=MOD;return x;}

lli n;
lli h[100010];
vector<pair<lli,lli> >v;
bool vis[100010];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    debug = 1;
#endif
    srand (time(NULL));

    slli(n);
    for(lli i=1;i<=n;i++)
        slli(h[i]);

    for(lli i=1;i<=n;i++)
        v.pb({h[i],i});

    sort(all(v));

    lli cur=0;
    lli ans=0;
    for(int i=0;i<n;)
    {
        vector<int> temp;
        if(v[i].S>=cur)
            temp.pb(v[i].S);
        i++;
        while(i<n && v[i].F==v[i-1].F)
        {

        if(v[i].S>=cur)temp.pb(v[i].S);
            i++;
        }

        int e=-1;
        for(int j=0;j<temp.size();j++)
        {
            if(temp[j]==cur+1)
            {
                cur++;
                ans++;
            }
            else
                e=temp[j];
        }
        if(e!=-1)
        {
            ans++;
            cur=e;
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);

const double EPS = 1e-4;
const lli MOD = 1000000007ll;
const lli HASHMOD[2]={1000000009ll,1100000009ll};
const double PI = 3.14159265359;
int INF = 2147483645;
lli INFINF = 9223372036854775807;
int debug = 0;

template <class T>T Max2(T a,T b){return a<b?b:a;}
template <class T>T Min2(T a,T b){return a<b?a:b;}
template <class T>T Max3(T a,T b,T c){return Max2(Max2(a,b),c);}
template <class T>T Min3(T a,T b,T c){return Min2(Min2(a,b),c);}
template <class T>T Max4(T a,T b,T c,T d){return Max2(Max2(a,b),Max2(c,d));}
template <class T>T Min4(T a,T b,T c,T d){return Min2(Min2(a,b),Min2(c,d));}

using namespace std;

lli bit_count(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
lli bit(lli _mask,lli _i){return (_mask&(1<<_i))==0?0:1;}
lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b){lli x=a+b;if(x>MOD)x-=MOD;return x;}
lli sub(lli a,lli b){lli x=a-b;if(x<0)x+=MOD;return x;}
lli mul(lli a,lli b){lli x=a*b;x%=MOD;return x;}

lli n;
lli h[100010];
vector<pair<lli,lli> >v;
bool vis[100010];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    debug = 1;
#endif
    srand (time(NULL));

    slli(n);
    for(lli i=1;i<=n;i++)
        slli(h[i]);

    for(lli i=1;i<=n;i++)
        v.pb({h[i],i});

    sort(all(v));

    lli cur=1;
    lli ans=0;
    for(int i=0;i<n;)
    {
        vis[v[i].S]=true;
        i++;
        while(v[i].F==v[i-1].F)
        {
            vis[v[i].S]=true;
            i++;
        }
        if(vis[cur])
        {
            while(vis[cur] && cur<=n)
                cur++;
            ans++;
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);

const double EPS = 1e-4;
const lli MOD = 1000000007ll;
const lli HASHMOD[2]={1000000009ll,1100000009ll};
const double PI = 3.14159265359;
int INF = 2147483645;
lli INFINF = 9223372036854775807;
int debug = 0;

template <class T>T Max2(T a,T b){return a<b?b:a;}
template <class T>T Min2(T a,T b){return a<b?a:b;}
template <class T>T Max3(T a,T b,T c){return Max2(Max2(a,b),c);}
template <class T>T Min3(T a,T b,T c){return Min2(Min2(a,b),c);}
template <class T>T Max4(T a,T b,T c,T d){return Max2(Max2(a,b),Max2(c,d));}
template <class T>T Min4(T a,T b,T c,T d){return Min2(Min2(a,b),Min2(c,d));}

using namespace std;

lli bit_count(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
lli bit(lli _mask,lli _i){return (_mask&(1<<_i))==0?0:1;}
lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b){lli x=a+b;if(x>MOD)x-=MOD;return x;}
lli sub(lli a,lli b){lli x=a-b;if(x<0)x+=MOD;return x;}
lli mul(lli a,lli b){lli x=a*b;x%=MOD;return x;}

lli n;
lli h[100010];
vector<pair<lli,lli> >v;
bool vis[100010];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    debug = 1;
#endif
    srand (time(NULL));

    slli(n);
    for(int i=1;i<=n;i++)
        slli(h[i]);

    for(int i=1;i<=n;i++)
        v.pb({h[i],i});
    sort(all(v));

    int cur=1;
    int ans=0;
    for(pair<lli,lli> it:v)
    {
        vis[it.S]=true;
        if(vis[cur])
        {
            while(vis[cur] && cur<=n)
                cur++;
            ans++;
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>

#define lli long long int
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);

const double EPS = 1e-4;
const lli MOD = 1000000007ll;
const lli HASHMOD[2]={1000000009ll,1100000009ll};
const double PI = 3.14159265359;
int INF = 2147483645;
lli INFINF = 9223372036854775807;
int debug = 0;

template <class T>T Max2(T a,T b){return a<b?b:a;}
template <class T>T Min2(T a,T b){return a<b?a:b;}
template <class T>T Max3(T a,T b,T c){return Max2(Max2(a,b),c);}
template <class T>T Min3(T a,T b,T c){return Min2(Min2(a,b),c);}
template <class T>T Max4(T a,T b,T c,T d){return Max2(Max2(a,b),Max2(c,d));}
template <class T>T Min4(T a,T b,T c,T d){return Min2(Min2(a,b),Min2(c,d));}

using namespace std;

lli bit_count(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
lli bit(lli _mask,lli _i){return (_mask&(1<<_i))==0?0:1;}
lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b){lli x=a+b;if(x>MOD)x-=MOD;return x;}
lli sub(lli a,lli b){lli x=a-b;if(x<0)x+=MOD;return x;}
lli mul(lli a,lli b){lli x=a*b;x%=MOD;return x;}

lli n;
lli h[100010];
vector<pair<lli,lli> >v;
bool vis[100010];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    debug = 1;
#endif
    srand (time(NULL));

    slli(n);
    for(lli i=1;i<=n;i++)
        slli(h[i]);

    for(lli i=1;i<=n;i++)
        v.pb({h[i],i});

    sort(all(v));

    lli cur=0;
    lli ans=0;
    for(int i=0;i<n;)
    {
        vector<int> temp;
        if(v[i].S>cur)
            temp.pb(v[i].S);
        i++;
        while(i<n && v[i].F==v[i-1].F)
        {
            if(v[i].S>cur)
                temp.pb(v[i].S);
            i++;
        }

        int e=-1;
        for(int j=0;j<temp.size();j++)
        {
            if(temp[j]==cur+1)
            {
                cur++;
                ans++;
            }
            else
                e=temp[j];
        }
        if(e!=-1)
        {
            ans++;
            cur=e;
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define scd( x ) scanf( "%lf" , &x )
#define scl( x ) scanf( "%ld" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%lf\n" , x )
#define pfl( x ) printf( "%ld\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define e2( x ) ( x )*( x )
#define r2( x ) sqrt( 1.0*( x ) )
#define cc( str1 , str2 ) strcmp( str1 , str2 )
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;

typedef long long ll ;
const int INF = (int)( INT_MAX/2 ) ;
const int N = (int)( 0 ) ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	sc( n ) ;
	stack<int> alt ;
	REP( i , n ){
		int h ;
		sc( h ) ;
		if( !alt.empty() ){
			int maxh = alt.top() ;
			while( ( !alt.empty() ) && ( alt.top() > h ) ){
				alt.pop() ;
			}
			alt.push( max( maxh , h ) ) ;
		}
		else{
			alt.push( h ) ;
		}
	}

	pf( SZ( alt ) ) ;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fr(i,a,b) for(int i = (a); i >= (b); i--)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define sc2( x , y ) scanf( "%d%d" , &x , &y )
#define scd( x ) scanf( "%.9f" , &x )
#define scl( x ) scanf( "%I64d" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%.9f\n" , x )
#define pfl( x ) printf( "%I64d\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define EPS (int)( 1e-6 )
#define e2( x ) ( x )*( x )
#define r2( x ) round( sqrt( 1.0*( x ) ) )
#define ones(x) __builtin_popcount(x)
#define MCM( a , b ) ( ( a*b )/( __gcd( a , b ) ) )
#define ddd cout << "despues" << endl ;
#define sss cout << "------------------" << endl ;
#define aaa cout << "antes" << endl ;
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define DEBUGL(x,n) cout << #x << " :" <<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32);
#define DEBUGC(x,n,m) cout << #x << " :" <<endl; REP(i,n)REP(j,m)cout<<x[i][j]<<char(j+1==m?10:32);
#define da( a , b ) ( (a)/(b) - ( (a) < 0 && (a)%(b) != 0 ) )
#define ceil_( a , b ) ( da( (a) , (b) ) + ((a)%(b) > 0) )
#define Mm greater<int>

typedef double db ;
typedef long double ld ;
typedef long long ll ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<ll> vl ;
typedef vector<bool> vb ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;
const int INF = (int)( INT_MAX - 100 ) ;
const ll mod = (int)( 1e+9 + 7 ) ;
const int N = (int)( 0 ) ;
//inline ll modulo( ll num ){ ( ( num %= mod ) += mod ) %= mod ; return num ; }
//inline ll pot( int b , int e ){ ll p = 1 ; REP( i , e ) p = (p*b)%mod ; return p ; }

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	cin >> n ;
	int a , h ;
	sc( a ) ;
	int k = 1 ;
	REP( i , n - 1 ){
		sc( h ) ;
		if( a <= h ){
			k ++ ;
			a = h ;
			continue ;
		}
	}
	cout << k ;

	return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fr(i,a,b) for(int i = (a); i >= (b); i--)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define sc2( x , y ) scanf( "%d%d" , &x , &y )
#define scd( x ) scanf( "%.9f" , &x )
#define scl( x ) scanf( "%I64d" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%.9f\n" , x )
#define pfl( x ) printf( "%I64d\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define EPS (int)( 1e-6 )
#define e2( x ) ( x )*( x )
#define r2( x ) round( sqrt( 1.0*( x ) ) )
#define ones(x) __builtin_popcount(x)
#define MCM( a , b ) ( ( a*b )/( __gcd( a , b ) ) )
#define ddd cout << "despues" << endl ;
#define sss cout << "------------------" << endl ;
#define aaa cout << "antes" << endl ;
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define DEBUGL(x,n) cout << #x << " :" <<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32);
#define DEBUGC(x,n,m) cout << #x << " :" <<endl; REP(i,n)REP(j,m)cout<<x[i][j]<<char(j+1==m?10:32);
#define da( a , b ) ( (a)/(b) - ( (a) < 0 && (a)%(b) != 0 ) )
#define ceil_( a , b ) ( da( (a) , (b) ) + ((a)%(b) > 0) )
#define Mm greater<int>

typedef double db ;
typedef long double ld ;
typedef long long ll ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<ll> vl ;
typedef vector<bool> vb ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;
const int INF = (int)( INT_MAX - 100 ) ;
const ll mod = (int)( 1e+9 + 7 ) ;
const int N = (int)( 0 ) ;
//inline ll modulo( ll num ){ ( ( num %= mod ) += mod ) %= mod ; return num ; }
//inline ll pot( int b , int e ){ ll p = 1 ; REP( i , e ) p = (p*b)%mod ; return p ; }

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	cin >> n ;
	vi h( n ) ;
	REP( i , n ) sc( h[ i ] ) ;
	multiset<int> s ;
	REP( i , n ) s.insert( h[ i ] ) ;
	int k = 0 , p = 0 ;
	REP( i , n ){
		if( h[ i ] == *s.begin() ) k ++ ;
		s.erase( s.find( h[ i ] ) ) ;
	}
	cout << k ;

	return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define scd( x ) scanf( "%lf" , &x )
#define scl( x ) scanf( "%ld" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%lf\n" , x )
#define pfl( x ) printf( "%ld\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define e2( x ) ( x )*( x )
#define r2( x ) sqrt( 1.0*( x ) )
#define cc( str1 , str2 ) strcmp( str1 , str2 )
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;

typedef long long ll ;
const int INF = (int)( INT_MAX/2 ) ;
const int N = (int)( 0 ) ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	sc( n ) ;
	vpii h( n ) ;
	REP( i , n ){
		sc( h[ i ].fi ) ;
		h[ i ].se = i ;
	}

	sort( all( h ) ) ;

	int cant ;
	int last = 0 ;
	int ans = 0 ;
	REP( pos , 1000 ){
		for( int i = pos ; i < n ; i++ ){
			if( h[ i ].se >= last ){
				cant ++ ;
				last = h[ i ].se ;
			}
		}
		ans = max( cant , ans ) ;
		cant = 0 ;
		last = 0 ;
	}
	/*
	REP( i , n ){
		DEBUG2( h[ i ].se , h[ i ].fi )
	}*/

	pf( ans ) ;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define scd( x ) scanf( "%lf" , &x )
#define scl( x ) scanf( "%ld" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%lf\n" , x )
#define pfl( x ) printf( "%ld\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define e2( x ) ( x )*( x )
#define r2( x ) sqrt( 1.0*( x ) )
#define cc( str1 , str2 ) strcmp( str1 , str2 )
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;

typedef long long ll ;
const int INF = (int)( INT_MAX/2 ) ;
const int N = (int)( 0 ) ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	sc( n ) ;

	int h ;
	int cant = 0 ;
	int ans = 1 ;
	int last = 0 ;
	REP( i , n ){
		sc( h ) ;
//		DEBUG2( last , h )
		if( last <= h ) cant ++ ;
		last = h ;
	}

	pf( cant ) ;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fr(i,a,b) for(int i = (a); i >= (b); i--)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define sc2( x , y ) scanf( "%d%d" , &x , &y )
#define scd( x ) scanf( "%.9f" , &x )
#define scl( x ) scanf( "%I64d" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%.9f\n" , x )
#define pfl( x ) printf( "%I64d\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define EPS (int)( 1e-6 )
#define e2( x ) ( x )*( x )
#define r2( x ) round( sqrt( 1.0*( x ) ) )
#define ones(x) __builtin_popcount(x)
#define MCM( a , b ) ( ( a*b )/( __gcd( a , b ) ) )
#define ddd cout << "despues" << endl ;
#define sss cout << "------------------" << endl ;
#define aaa cout << "antes" << endl ;
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define DEBUGL(x,n) cout << #x << " :" <<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32);
#define DEBUGC(x,n,m) cout << #x << " :" <<endl; REP(i,n)REP(j,m)cout<<x[i][j]<<char(j+1==m?10:32);
#define da( a , b ) ( (a)/(b) - ( (a) < 0 && (a)%(b) != 0 ) )
#define ceil_( a , b ) ( da( (a) , (b) ) + ((a)%(b) > 0) )
#define Mm greater<int>

typedef double db ;
typedef long double ld ;
typedef long long ll ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<ll> vl ;
typedef vector<bool> vb ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;
const int INF = (int)( INT_MAX - 100 ) ;
const ll mod = (int)( 1e+9 + 7 ) ;
const int N = (int)( 0 ) ;
//inline ll modulo( ll num ){ ( ( num %= mod ) += mod ) %= mod ; return num ; }
//inline ll pot( int b , int e ){ ll p = 1 ; REP( i , e ) p = (p*b)%mod ; return p ; }

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	cin >> n ;
	int a , h ;
	sc( a ) ;
	int k = 1 ;
	REP( i , n - 1 ){
		sc( h ) ;
		if( a <= h ){
			k ++ ;
			a = h ;
			continue ;
		}
		a = h ;
	}
	cout << k ;

	return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define scd( x ) scanf( "%lf" , &x )
#define scl( x ) scanf( "%ld" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%lf\n" , x )
#define pfl( x ) printf( "%ld\n" , x )
#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define SZ(a) int(a.size())
#define pb push_back
#define pi acos(-1.0)
#define e2( x ) ( x )*( x )
#define r2( x ) sqrt( 1.0*( x ) )
#define cc( str1 , str2 ) strcmp( str1 , str2 )
#define DEBUG( x ) cerr <<  #x << "=" << x << endl;
#define DEBUG2( x , y ) cerr << #x << "=" << x << " " << #y << "=" << y << endl;

typedef long long ll ;
const int INF = (int)( INT_MAX/2 ) ;
const int N = (int)( 0 ) ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;

int main()
{
//	ios_base::sync_with_stdio(0);
	int n ;
	sc( n ) ;
	int cant = 0 ;
	vpii h( n ) ;
	REP( i , n ){
		sc( h[ i ].fi ) ;
		h[ i ].se = i ;
	}

	sort( all( h ) ) ;

	int last = 0 ;
	REP( i , n ){
		if( h[ i ].se >= last ) cant ++ ;
		last = h[ i ].se ;
	}

	pf( cant ) ;

	return 0;
}


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

typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REPN(i,a,b) for(int i=a;i<=b;i++)
#define reset(a,b) memset(a,b,sizeof(a))
#define sc(x) scanf("%d",&x)

int arr[100010];
int maxi[100010];
int mini[100010];

int main(void){
    int n;
    sc(n);
    FOR(i,n){
        sc(arr[i]);
        if(i){
            maxi[i] = max(maxi[i], arr[i]);
        }else{
            maxi[i] = arr[i];
        }
    }
    mini[n] = INF;
    for(int i = n-1;i>=0;i--){
        mini[i] = min(mini[i+1], arr[i]);
    }

    int tot = 0;
    FOR(i,n){
        if(mini[i+1] >= maxi[i])tot++;
    }

    cout << tot << endl;

    return 0;
}


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

typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REPN(i,a,b) for(int i=a;i<=b;i++)
#define reset(a,b) memset(a,b,sizeof(a))
#define sc(x) scanf("%d",&x)

int arr[100010];
int dup[100010];
map<int,int> latest;
int main(void){
    int n;
    sc(n);
    FOR(i,n){
        sc(arr[i]);
        dup[i] = arr[i];
        latest[arr[i]] = i;
    }

    sort(arr, arr + n);
    int cur = -1;
    int tot = 0;
    FOR(i,n){
        int pos = latest[arr[i]];
        if(pos > cur){
            tot++;
            cur = pos;
        }
    }

    cout << tot << endl;

    return 0;
}


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

typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REPN(i,a,b) for(int i=a;i<=b;i++)
#define reset(a,b) memset(a,b,sizeof(a))
#define sc(x) scanf("%d",&x)

int arr[100010];
int maxi[100010];
int mini[100010];

int main(void){
    int n;
    sc(n);
    FOR(i,n){
        sc(arr[i]);
        if(i){
            maxi[i] = max(maxi[i-1], arr[i]);
        }else{
            maxi[i] = arr[i];
        }
    }
    mini[n] = INF;
    for(int i = n-1;i>=0;i--){
        mini[i] = min(mini[i+1], arr[i]);
    }

    int tot = 0;
    FOR(i,n){
        if(mini[i+1] >= maxi[i])tot++;
    }

    cout << tot << endl;

    return 0;
}


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

typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REPN(i,a,b) for(int i=a;i<=b;i++)
#define reset(a,b) memset(a,b,sizeof(a))
#define sc(x) scanf("%d",&x)

int arr[100010];

map<int,int> latest;
map<int, int> smaller;

int main(void){
    int n;
    sc(n);
    FOR(i,n){
        sc(arr[i]);
        latest[arr[i]] = i;
    }

    int rem;
    for(map<int,int>::iterator it = latest.begin();it!=latest.end();it++){
        if(it == latest.begin()){
            smaller[it->first] = -1;
            rem = it->second;
        }else{
            smaller[it->first] = rem;

            rem = max(it->second, rem);
        }
    }

    int cur = 0;
    int tot = 0;
    while(cur < n){
        int pos = smaller[arr[cur]];
        if(pos > cur){
            cur = pos;
        }
        cur++;
        tot++;
    }

    cout << tot << endl;

    return 0;
}


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

typedef long long ll;
typedef long long LL;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#ifdef DEBUG
    #define cek(x) cout<<x
#else
    #define cek(x) if(false){}
#endif // DEBUG

#define fi first
#define se second
#define INF 1000000000
#define INFLL 1000000000000000000LL
#define EPS 1e-9
#define PI acos(-1.0)
#define pb push_back
#define TC() while(tc--)
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORN(i,n) for(int i=0;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REPN(i,a,b) for(int i=a;i<=b;i++)
#define reset(a,b) memset(a,b,sizeof(a))
#define sc(x) scanf("%d",&x)

int arr[100010];

map<int,int> latest;
map<int, int> smaller;

int main(void){
    int n;
    sc(n);
    FOR(i,n){
        sc(arr[i]);
        latest[arr[i]] = i;
    }

    int rem;
    for(map<int,int>::iterator it = latest.begin();it!=latest.end();it++){
        if(it == latest.begin()){
            smaller[it->first] = -1;
            rem = it->second;
        }else{
            smaller[it->first] = rem;

            rem = max(it->second, rem);
        }
    }

    int cur = 0;
    int tot = 0;
    while(cur < n){
        int pos = smaller[arr[cur]];
        if(pos > cur){
            int maxi = pos;
            int tempcur = cur;
            int temppos;
            while(true){
                REP(i,tempcur, pos)
                    maxi = smaller[arr[i]];
                if(maxi <= pos)break;
                pos = maxi;
                tempcur = pos;
            }
            cur = pos;
        }
        cur++;
        tot++;
    }

    cout << tot << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007

string S,M[100001];
int N,K[100001];

struct Node{
  int child[26];
  int link;
  vector<int>tokens;
  Node(){
    for (int i=0;i<26;i++)
      child[i]=0;
    link=0;
  }
};
typedef struct Node Node;

vector<Node>trie;

int getchild(int node, int target){
  if (trie[node].child[target]==0){
    trie[node].child[target]=trie.size();
    trie.emplace_back();
  }
  return trie[node].child[target];
}

void insert(string s, int ind){
  int cur=0;
  for (int i=0;i<(int)s.length();i++)
    cur=getchild(cur,(int)(s[i]-'a'));
  trie[cur].tokens.push_back(ind);
}
vector<int>ind[100001]; //index(of last char)
int store[100001][26];

int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>S>>N;
  trie.emplace_back();
  for (int i=0;i<N;i++){
    cin>>K[i]>>M[i];
    insert(M[i],i);
  }
  queue<int>q;
  q.push(0);
  vector<int>v;
  while (!q.empty()){
    int X=q.front();
    q.pop();
    if (X!=0)
      v.push_back(X);
    for (int i=0;i<26;i++)
      if (trie[X].child[i]!=0)
	q.push(trie[X].child[i]);
  }
  for (int i=0;i<=100000;i++)
    for (int j=0;j<26;j++)
      store[i][j]=-1;
  for (int i:v){
    for (int j=0;j<26;j++){
      int cur=trie[i].link;
      while (trie[cur].child[j]==0 && cur!=0){
	if (store[cur][j]!=-1){
	  cur=store[cur][j];
	  break;
	}
	cur=trie[cur].link;
      }
      int temp=trie[i].link;
      while (trie[temp].child[j]==0 && temp!=0){
	if (store[temp][j]!=-1)
	  break;
	store[temp][j]=cur;
	temp=trie[temp].link;
      }
      cur=trie[cur].child[j];
      if (trie[i].child[j]==0)
	trie[i].child[j]=cur;
      else
	trie[trie[i].child[j]].link=cur;
    }
  }

  for (int i=0;i<(int)v.size();i++)
    trie[v[i]].tokens.insert(trie[v[i]].tokens.end(),trie[trie[v[i]].link].tokens.begin(),trie[trie[v[i]].link].tokens.end());

  /*
    for (int i=1;i<(int)trie.size();i++){
    for (int x:trie[i].tokens)
      cout<<x<<' ';
    cout<<endl;
  }
  */
  int cur=0;
  for (int i=0;i<(int)S.length();i++){
    cur=trie[cur].child[(int)S[i]-'a'];
    for (int j:trie[cur].tokens)
      ind[j].push_back(i);
  }
  for (int i=0;i<N;i++){
    if ((int)ind[i].size()<K[i]){
      cout<<-1<<endl;
      continue;
    }
    int ans=INF;
    for (int j=0;j+K[i]-1<(int)ind[i].size();j++)
      ans=min(ans,ind[i][j+K[i]-1]-ind[i][j]);
    cout<<ans+(int)M[i].length()<<endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007

string S,M[100001];
int N,K[100001];

struct Node{
  int child[26];
  int link;
  vector<int>tokens;
  Node(){
    for (int i=0;i<26;i++)
      child[i]=0;
    link=0;
  }
};
typedef struct Node Node;

vector<Node>trie;

int getchild(int node, int target){
  if (trie[node].child[target]==0){
    trie[node].child[target]=trie.size();
    trie.emplace_back();
  }
  return trie[node].child[target];
}

void insert(string s, int ind){
  int cur=0;
  for (int i=0;i<(int)s.length();i++)
    cur=getchild(cur,(int)(s[i]-'a'));
  trie[cur].tokens.push_back(ind);
}
vector<int>ind[10001]; //index(of last char)

int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>S>>N;
  trie.emplace_back();
  for (int i=0;i<N;i++){
    cin>>K[i]>>M[i];
    insert(M[i],i);
  }
  queue<int>q;
  q.push(0);
  vector<int>v;
  while (!q.empty()){
    int X=q.front();
    q.pop();
    if (X!=0)
      v.push_back(X);
    for (int i=0;i<26;i++)
      if (trie[X].child[i]!=0)
	q.push(trie[X].child[i]);
  }
  for (int i:v){
    for (int j=0;j<26;j++){
      int cur=trie[i].link;
      while (trie[cur].child[j]==0 && cur!=0)
	cur=trie[cur].link;
      cur=trie[cur].child[j];
      if (trie[i].child[j]==0)
	trie[i].child[j]=cur;
      else
	trie[trie[i].child[j]].link=cur;
    }
  }
  for (int i=0;i<(int)v.size();i++)
    trie[v[i]].tokens.insert(trie[v[i]].tokens.end(),trie[trie[v[i]].link].tokens.begin(),trie[trie[v[i]].link].tokens.end());
  int cur=0;
  for (int i=0;i<(int)S.length();i++){
    cur=getchild(cur,(int)S[i]-'a');
    for (int j:trie[cur].tokens)
      ind[j].push_back(i);
  }
  for (int i=0;i<N;i++){
    if ((int)ind[i].size()<K[i]){
      cout<<-1<<endl;
      continue;
    }
    int ans=INF;
    for (int j=0;j+K[i]-1<(int)ind[i].size();j++)
      ans=min(ans,ind[i][j+K[i]-1]-ind[i][j]);
    cout<<ans+(int)M[i].length()<<endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007

string S,M[100001];
int N,K[100001];

struct Node{
  int child[26];
  int link;
  vector<int>tokens;
  Node(){
    for (int i=0;i<26;i++)
      child[i]=0;
    link=0;
  }
};
typedef struct Node Node;

vector<Node>trie;

int getchild(int node, int target){
  if (trie[node].child[target]==0){
    trie[node].child[target]=trie.size();
    trie.emplace_back();
  }
  return trie[node].child[target];
}

void insert(string s, int ind){
  int cur=0;
  for (int i=0;i<(int)s.length();i++)
    cur=getchild(cur,(int)(s[i]-'a'));
  trie[cur].tokens.push_back(ind);
}
vector<int>ind[100001]; //index(of last char)

int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>S>>N;
  trie.emplace_back();
  for (int i=0;i<N;i++){
    cin>>K[i]>>M[i];
    insert(M[i],i);
  }
  queue<int>q;
  q.push(0);
  vector<int>v;
  while (!q.empty()){
    int X=q.front();
    q.pop();
    if (X!=0)
      v.push_back(X);
    for (int i=0;i<26;i++)
      if (trie[X].child[i]!=0)
	q.push(trie[X].child[i]);
  }
  for (int i:v){
    for (int j=0;j<26;j++){
      int cur=trie[i].link;
      while (trie[cur].child[j]==0 && cur!=0)
	cur=trie[cur].link;
      cur=trie[cur].child[j];
      if (trie[i].child[j]==0)
	trie[i].child[j]=cur;
      else
	trie[trie[i].child[j]].link=cur;
    }
  }
  for (int i=0;i<(int)v.size();i++)
    trie[v[i]].tokens.insert(trie[v[i]].tokens.end(),trie[trie[v[i]].link].tokens.begin(),trie[trie[v[i]].link].tokens.end());
  /*
    for (int i=1;i<(int)trie.size();i++){
    for (int x:trie[i].tokens)
      cout<<x<<' ';
    cout<<endl;
  }
  */
  int cur=0;
  for (int i=0;i<(int)S.length();i++){
    cur=trie[cur].child[(int)S[i]-'a'];
    for (int j:trie[cur].tokens)
      ind[j].push_back(i);
  }
  for (int i=0;i<N;i++){
    if ((int)ind[i].size()<K[i]){
      cout<<-1<<endl;
      continue;
    }
    int ans=INF;
    for (int j=0;j+K[i]-1<(int)ind[i].size();j++)
      ans=min(ans,ind[i][j+K[i]-1]-ind[i][j]);
    cout<<ans+(int)M[i].length()<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define para(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define paraIni(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rpara(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define unicos(X) (X).erase(unique(all(X)),(X).end())
#define NL <<"\n"
#define EPS 1e-6
#define MOD 1000000007
#define iinf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f

using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
const int K = 26;

struct Vertex {
    int next[K];
    bool leaf = false;
    // ancesrtor
    int p = -1;
    // character of the ancestor
    char pch;
    // suffix link
    int link = -1;
    int exit = -1;
    int go[K];

    Vertex(int p=-1, char ch='$') : p(p), pch(ch) {
        fill(begin(next), end(next), -1);
        fill(begin(go), end(go), -1);
    }
};

vector<Vertex> t(1);

map <string, int> m;
map <int, string> m2;
void add_string(string const& s) {
    int v = 0;
    for (char ch : s) {
        int c = ch - 'a';
        if (c < 0)
            c = ch - 'A';
        if (t[v].next[c] == -1) {
            t[v].next[c] = t.size();
            t.emplace_back(v, ch);
        }
        v = t[v].next[c];
    }
    m[s] = v;
    m2[v] = s;
    t[v].leaf = true;
    // printf("%s\n", );
    // cout << s << endl;
    // cout << v << endl;

}

int go(int v, char ch);

int get_link(int v) {
    if (t[v].link == -1) {
        if (v == 0 || t[v].p == 0)
            t[v].link = 0;
        else
            t[v].link = go(get_link(t[v].p), t[v].pch);

    }

    return t[v].link;
}
// int getExit(int v)
// {
//     if (t[v].exit != -1)
//         return t[v].exit;
//     int u = t[v].link;
//     while(u!= 0)
//     {
//         if (t[u].leaf)
//         {
//             t[v].exit = u;
//             break;
//         }
//         u = t[u].link;
//     }
//     return t[v].exit = u;
// }
int go(int v, char ch) {
    int c = ch - 'a';
    if (t[v].go[c] == -1) {
        if (t[v].next[c] != -1)
            t[v].go[c] = t[v].next[c];
        else
            t[v].go[c] = v == 0 ? 0 : go(get_link(v), ch);
    }
    return t[v].go[c];
}

int getExit(int v){
    // cout << v << endl;
    // if (v == 0 || t[v].p == 0)
    //     return -1;
    if(t[v].exit==-1){
        int u=get_link(v);
        if(u!=0 && t[u].leaf) t[v].exit=u;
        else t[v].exit = v==0 ? 0 : getExit(u);
    }
    return t[v].exit;
}
// set <string> visitados;
map <string, int> visitados;
vector <pair<string, int>> entrada;
map <string, pair<int,int>> contaEntrada;
vector<int> posicoesOcorrencias[112345];
int percorreArvore(string s)
{
    int v = 0;
    for (int i = 0; i < s.size(); ++i)
    {
        // cout << s[i] << " " << v << endl;
        // int u = s[i]-'a';
        // if (u < 0)
        //     u = s[i] - 'A';
        v = go(v, s[i]);
        if (t[v].leaf )
        {
            visitados[m2[v]]=1;
            posicoesOcorrencias[contaEntrada[m2[v]].se].pb(i);
        }

        int v2 =getExit(v);
        while (v2 != 0 && v2 != -1)
        {

            visitados[m2[v2]]=1;
            posicoesOcorrencias[contaEntrada[m2[v2]].se].pb(i);
            v2 =getExit(v2);
        }
        // se o nó em que estou tem filho s[i]
        // if (t[v].next[u]!= -1)
        // {
        //     v = t[v].next[u];
        //     // cout << v << endl;
        //     if (t[v].leaf)
        //     {
        //         visitados[m2[v]]=1;
        //         posicoesOcorrencias[contaEntrada[m2[v]].se].pb(i);
        //     }
        //     int v2 =getExit(v);
        //     while (v2 != -1)
        //     {

        //         visitados[m2[v2]]=1;
        //         posicoesOcorrencias[contaEntrada[m2[v2]].se].pb(i);
        //         v2 =getExit(v2);
        //     }
        // }
        // else
        // {
        //     while (t[v].next[u]== -1 && v!=0)
        //     {

        //         // v = t[v].link;
        //         // cout << v << endl;
        //         // printf("1\n");
        //     }
        //     if (t[v].next[u] != -1)
        //         v = t[v].next[u];
        //     if (t[v].leaf)
        //     {
        //         visitados[m2[v]]=1;
        //         posicoesOcorrencias[contaEntrada[m2[v]].se].pb(i);
        //         // v = t[v].exit;
        //     }
        //     int v2 =getExit(v);
        //     while (v2 != -1)
        //     {

        //         visitados[m2[v2]]=1;
        //         posicoesOcorrencias[contaEntrada[m2[v2]].se].pb(i);
        //         v2 =getExit(v2);
        //     }

        // }

    }

}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    string s;
    cin >> s;
    int n;
    cin >> n;
    t[0].exit=-1;
    // map <string, int>
    for (int i = 0; i < n; ++i)
    {
        int sz;
        cin >> sz;
        string s;
        cin >> s;
        add_string(s);
        entrada.pb(mp(s,sz));
        contaEntrada[s]=mp(sz,i);
    }
    // for (int v = 0; v < t.size(); ++v)
    // {
    //     get_link(v);
    //     // printf("%d ",v );
    //     // printf("%c ",t[v].pch );
    //     // printf("%d ",t[v].exit );
    //     // printf("\n");
    // }
    // for (int v = 0; v < t.size(); ++v)
    // {
    //     getExit(v);
    //     // printf("%d ",v );
    //     // printf("%c ",t[v].pch );
    //     // printf("%d ",t[v].exit );
    //     // printf("\n");
    // }

    percorreArvore(s);
    for (int i = 0; i < entrada.size(); ++i)
    {
        // printf("%s\n", );
        // cout << entrada[i].fi << endl;
        // cout << posicoesOcorrencias[i].size() << endl;
        if(posicoesOcorrencias[i].size() < entrada[i].se )
        {
            // printf("%d\n",-1 );
            cout << -1 << endl;
        }
        else
        {
            int tam = 0;
            int k = entrada[i].se-1;
            tam =posicoesOcorrencias[i][k] - posicoesOcorrencias[i][0];
            // printf("%d\n",tam );
            for (int j = 1; j+k < posicoesOcorrencias[i].size(); ++j)
            {
                if (posicoesOcorrencias[i][j+k] - posicoesOcorrencias[i][j]  < tam)
                    tam = posicoesOcorrencias[i][j+k] - posicoesOcorrencias[i][j];
            }
            cout <<tam+ entrada[i].fi.size() << endl;
        }
        // for (int j = 0; j < posicoesOcorrencias[i].size(); ++j)
        // {
        //     cout << m2[posicoesOcorrencias[i][j]] << " " ;
        //     // printf("%d ",posicoesOcorrencias[i][j] );
        // }
        // printf("\n");
    }
    // for (int i = 0; i < entrada.size(); ++i)
    // {
    //     cout << entrada[i]<<endl;
    //     if (visitados.count(entrada[i]))
    //         cout << 'y'<<endl;
    //     else
    //         cout << 'n'<<endl;
    // }

    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define para(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define paraIni(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rpara(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define unicos(X) (X).erase(unique(all(X)),(X).end())
#define NL <<"\n"
#define EPS 1e-6
#define MOD 1000000007
#define iinf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f

using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
const int K = 26;

struct Vertex {
    int next[K];
    bool leaf = false;
    // ancesrtor
    int p = -1;
    // character of the ancestor
    char pch;
    // suffix link
    int link = -1;
    int exit = -1;
    int go[K];

    Vertex(int p=-1, char ch='$') : p(p), pch(ch) {
        fill(begin(next), end(next), -1);
        fill(begin(go), end(go), -1);
    }
};

vector<Vertex> t(1);

map <string, int> m;
map <int, string> m2;
void add_string(string const& s) {
    int v = 0;
    for (char ch : s) {
        int c = ch - 'a';
        if (c < 0)
            c = ch - 'A';
        if (t[v].next[c] == -1) {
            t[v].next[c] = t.size();
            t.emplace_back(v, ch);
        }
        v = t[v].next[c];
    }
    m[s] = v;
    m2[v] = s;
    t[v].leaf = true;

}

int go(int v, char ch);

int get_link(int v) {
    if (t[v].link == -1) {
        if (v == 0 || t[v].p == 0)
            t[v].link = 0;
        else
            t[v].link = go(get_link(t[v].p), t[v].pch);

        int u = t[v].link;
        while(u!= 0)
        {
            if (t[u].leaf)
            {
                t[v].exit = u;
                break;
            }
            u = t[u].link;
        }
    }
    return t[v].link;
}

int go(int v, char ch) {
    int c = ch - 'a';
    if (c < 0)
        c = ch - 'A';
    if (t[v].go[c] == -1) {
        if (t[v].next[c] != -1)
            t[v].go[c] = t[v].next[c];
        else
            t[v].go[c] = v == 0 ? 0 : go(get_link(v), ch);
    }
    return t[v].go[c];
}

// set <string> visitados;
map <string, int> visitados;
vector <pair<string, int>> entrada;
map <string, pair<int,int>> contaEntrada;
vector<int> posicoesOcorrencias[112345];
int percorreArvore(string s)
{
    int v = 0;
    for (int i = 0; i < s.size(); ++i)
    {
        int u = s[i]-'a';
        if (u < 0)
            u = s[i] - 'A';
        // se o nó em que estou tem filho s[i]
        if (t[v].next[u]!= -1)
        {
            v = t[v].next[u];
            if (t[v].leaf)
            {
                visitados[m2[v]]=1;
                posicoesOcorrencias[contaEntrada[m2[v]].se].pb(i);
            }
            int v2 = t[v].exit;
            while (v2 != -1)
            {

                visitados[m2[v2]]=1;
                posicoesOcorrencias[contaEntrada[m2[v2]].se].pb(i);
                v2 = t[v2].exit;
            }
        }
        else
        {
            while (t[v].next[u]== -1 && v!=0)
            {
                v = t[v].link;
                // printf("1\n");
            }
            if (t[v].next[u] != -1)
                v = t[v].next[u];
            if (t[v].leaf)
            {
                visitados[m2[v]]=1;
                posicoesOcorrencias[contaEntrada[m2[v]].se].pb(i);
                // v = t[v].exit;
            }
            int v2 = t[v].exit;
            while (v2 != -1)
            {

                visitados[m2[v2]]=1;
                posicoesOcorrencias[contaEntrada[m2[v2]].se].pb(i);
                v2 = t[v2].exit;
            }

        }

    }

}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    string s;
    cin >> s;
    int n;
    cin >> n;
    // map <string, int>
    for (int i = 0; i < n; ++i)
    {
        int sz;
        cin >> sz;
        string s;
        cin >> s;
        add_string(s);
        entrada.pb(mp(s,sz));
        contaEntrada[s]=mp(sz,i);
    }
    for (int v = 0; v < t.size(); ++v)
    {
        get_link(v);
        // printf("%d ",v );
        // printf("%c ",t[v].pch );
        // printf("%d ",t[v].exit );
        // printf("\n");
    }

    percorreArvore(s);
    for (int i = 0; i < entrada.size(); ++i)
    {
        if(posicoesOcorrencias[i].size() < entrada[i].se )
            printf("%d\n",-1 );
        else
        {
            int tam = 0;
            int k = entrada[i].se-1;
            tam =posicoesOcorrencias[i][k] - posicoesOcorrencias[i][0];
            // printf("%d\n",tam );
            for (int j = 1; j+k < posicoesOcorrencias[i].size(); ++j)
            {
                if (posicoesOcorrencias[i][j+k] - posicoesOcorrencias[i][j]  < tam)
                    tam = posicoesOcorrencias[i][j+k] - posicoesOcorrencias[i][j];
            }
            printf("%d\n",tam+ entrada[i].fi.size() );
        }
        // for (int j = 0; j < posicoesOcorrencias[i].size(); ++j)
        // {
        //     printf("%d ",posicoesOcorrencias[i][j] );
        // }
        // printf("\n");
    }
    // for (int i = 0; i < entrada.size(); ++i)
    // {
    //     cout << entrada[i]<<endl;
    //     if (visitados.count(entrada[i]))
    //         cout << 'y'<<endl;
    //     else
    //         cout << 'n'<<endl;
    // }

    return 0;
}