#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <complex>
#include <stack>
#define LL long long int
#define dob double
using namespace std;
const int N = 100010;
struct Data{int rk,l,a,r,len;}s[N],f[N];
int n,m,T[N],Ans;
int gi()
{
int x=0,res=1;char ch=getchar();
while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)res*=-1;ch=getchar();}
while(ch<=‘9‘&&ch>=‘0‘)x=x*10+ch-48,ch=getchar();
return x*res;
}
inline bool cmpa(const Data &a,const Data &b){return a.a<b.a;}
inline int lb(int k){return k&-k;}
inline void update(int x,int mx){for(;x<=n;x+=lb(x))T[x]=max(T[x],mx);}
inline int query(int x){int ans=0;for(;x;x-=lb(x))ans=max(ans,T[x]);return ans;}
inline void clean(int x){for(;x<=n;x+=lb(x))T[x]=0;}
inline void merge(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
int x=l,y=mid+1,i=l;
Data f[N];
while(x<=mid && y<=r){
if(s[x].r<=s[y].r)
f[i++]=s[x++];
else f[i++]=s[y++];
}
while(x<=mid)f[i++]=s[x++];
while(y<=r)f[i++]=s[y++];
for(i=l;i<=r;++i)s[i]=f[i];
}
inline void CDQ(int l,int r)
{
if(l==r){s[l].len=max(s[l].len,1);return;}
int mid=(l+r)>>1;
int x=l,y=mid+1;
for(int i=l;i<=r;++i)
if(s[i].rk<=mid)f[x++]=s[i];
else f[y++]=s[i];
for(int i=l;i<=r;++i)s[i]=f[i];
CDQ(l,mid);merge(l,mid);
x=l;y=mid+1;
while(x<=mid && y<=r){
if(s[x].r<=s[y].a)
update(s[x].a,s[x].len),++x;
else s[y].len=max(s[y].len,query(s[y].l)+1),++y;
}
while(y<=r)s[y].len=max(s[y].len,query(s[y].l)+1),++y;
for(int i=l;i<=mid;++i)clean(s[i].a);
CDQ(mid+1,r);merge(mid+1,r);
}
int main()
{
n=gi();m=gi();
for(int i=1;i<=n;++i)
s[i].rk=i,s[i].l=s[i].r=s[i].a=gi(),s[i].len=0;
for(int i=1;i<=m;++i){
int x=gi(),y=gi();
s[x].l=min(s[x].l,y);
s[x].r=max(s[x].r,y);
}
sort(s+1,s+n+1,cmpa);
CDQ(1,n);
for(int i=1;i<=n;++i)Ans=max(Ans,s[i].len);
printf("%d\n",Ans);
return 0;
}