#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>#define ll long longusingnamespacestd;
constint maxn=200005;
int f[maxn],l[maxn],a[maxn],r[maxn],next[maxn],d[maxn],g[maxn*10],g1[maxn];
int n,i,t,j,k,m,num,z,mx;
ll ans,x,y;
bool bz[maxn];
void change(int l,int r,int v){
int mid=(l+r)/2;
if (l==r){
if (y<0) g[v]=mx;
elseif (g[v]!=mx)g[v]=d[g[v]];
else g[v]=y;return;
}
if (mid>=x) change(l,mid,v*2);
else change(mid+1,r,v*2+1);
g[v]=min(g[v*2+1],g[v*2]);
}
int main(){
freopen("survey.in","r",stdin);freopen("survey.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(g,127,sizeof(g));mx=g[1];
for (i=1;i<=m;i++)
next[i]=n+1;
for (i=n;i>=1;i--) d[i]=next[a[i]],next[a[i]]=i;
for (i=1;i<=m;i++){
scanf("%d%d",&l[i],&r[i]);
if (!l[i]) bz[i]=true,num++;
}
i=1;
for (j=1;j<=n;j++){
f[a[j]]++;
if (!g1[a[j]]) g1[a[j]]=j;
if (f[a[j]]>=l[a[j]] && !bz[a[j]]) {
num++,bz[a[j]]=true,x=a[j],y=g1[a[j]],change(1,m,1);
}
if (f[a[j]]>l[a[j]] && l[a[j]]){
x=a[j],y=0,change(1,m,1);g1[a[j]]=d[g1[a[j]]];
}
while (f[a[j]]>r[a[j]]){
f[a[i]]--;
if (g1[a[i]]==i) g1[a[i]]=d[i];
if (f[a[i]]<l[a[i]] && bz[a[i]] && l[a[i]])y=-1,x=a[i],change(1,m,1),num--,bz[a[i]]=false;
i++;
}
if (num==m){
x=g[1],y=i,ans+=x-y+1;
}
}
printf("%lld\n",ans);
}