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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n,m;cin>>n>>m;
  vector<ll>p(n);
  vector<vector<ll>> g(n);
  rep(i,0,n){
    cin>>p[i];
    p[i]--;
  }
  rep(i,0,m){
    ll u,v;cin>>u>>v;
    u--;v--;
    g[v].PB(u);
  }
  set<ll>st;
  rep(i,0,g[p[n-1]].size()){
    //cout<<i spa g[p[n-1]][i]<<endl;
    st.insert(g[p[n-1]][i]);
  }
  ll cnt=0;
  rrep(i,n-1,0){
    //cout<<i<<endl;
    if(st.find(p[i])!=st.end())cnt++;
    else{
      set<ll>st2;
      rep(j,0,g[p[i]].size()){
        //cout<<i spa j spa g[p[i]][j]<<endl;
        st2.insert(g[p[i]][j]);
      }
      for(auto itr=st.begin();itr!=st.end();){
        if(st2.find(*itr)==st2.end())itr=st.erase(itr);
        else itr++;
      }
      if(st.empty())break;
    }
  }
  cout<<cnt<<endl;
  return 0;
}


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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n,m;cin>>n>>m;
  vector<ll>p(n);
  vector<vector<ll>> g(n);
  rep(i,0,n){
    cin>>p[i];
    p[i]--;
  }
  rep(i,0,m){
    ll u,v;cin>>u>>v;
    u--;v--;
    g[v].PB(u);
  }
  set<ll>st;
  rep(i,0,g[p[n-1]].size()){
    //cout<<i spa g[p[n-1]][i]<<endl;
    st.insert(g[p[n-1]][i]);
  }
  ll cnt=0;
  rrep(i,n-1,0){
    //cout<<i<<endl;
    if(st.find(p[i])!=st.end())cnt++;
    else{
      set<ll>st2;
      rep(j,0,g[p[i]].size()){
        //cout<<i spa j spa g[p[i]][j]<<endl;
        st2.insert(g[p[i]][j]);
      }
      for(auto z:st){
        if(st2.find(z)==st2.end())st.erase(z);
      }
      if(st.empty())break;
    }
  }
  cout<<cnt<<endl;
  return 0;
}


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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n,m;cin>>n>>m;
  vector<ll>p(n);
  vector<vector<ll>> g(n);
  rep(i,0,n){
    cin>>p[i];
    p[i]--;
  }
  rep(i,0,m){
    ll u,v;cin>>u>>v;
    u--;v--;
    g[v].PB(u);
  }
  set<ll>st;
  rep(i,0,g[p[n-1]].size())st.insert(g[p[n-1]][i]);
  ll cnt=0;
  rrep(i,n-1,0){
    if(st.count(p[i]))cnt++;
    else{
      set<ll>st2;
      rep(j,0,g[p[i]].size()){
        st2.insert(g[p[i]][j]);
      }
      for(auto z:st){
        if(!st2.count(z))st.erase(z);
      }
    }
  }
  cout<<cnt<<endl;
  return 0;
}


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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n,m;cin>>n>>m;
  vector<ll>p(n);
  vector<vector<ll>> g(n);
  rep(i,0,n){
    cin>>p[i];
    p[i]--;
  }
  rep(i,0,m){
    ll u,v;cin>>u>>v;
    u--;v--;
    g[v].PB(u);
  }
  set<ll>st;
  rep(i,0,g[p[n-1]].size()){
    //cout<<i spa g[p[n-1]][i]<<endl;
    st.insert(g[p[n-1]][i]);
  }
  ll cnt=0;
  rrep(i,n-1,0){
    //cout<<i<<endl;
    if(st.count(p[i]))cnt++;
    else{
      set<ll>st2;
      rep(j,0,g[p[i]].size()){
        //cout<<i spa j spa g[p[i]][j]<<endl;
        st2.insert(g[p[i]][j]);
      }
      for(auto z:st){
        if(st2.find(z)==st2.end())st.erase(z);
      }
      if(st.empty())break;
    }
  }
  cout<<cnt<<endl;
  return 0;
}


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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n,m;cin>>n>>m;
  vector<ll>p(n);
  vector<vector<ll>> g(n);
  rep(i,0,n){
    cin>>p[i];
    p[i]--;
  }
  rep(i,0,m){
    ll u,v;cin>>u>>v;
    u--;v--;
    g[v].PB(u);
  }
  set<ll>st;
  rep(i,0,g[p[n-1]].size())st.insert(g[p[n-1]][i]);
  ll cnt=0;
  rrep(i,n-1,0){
    if(st.count(p[i]))cnt++;
    else{
      set<ll>st2;
      rep(j,0,g[p[i]].size()){
        st2.insert(g[p[i]][j]);
      }
      for(auto z:st){
        if(!st2.count(z))st.erase(z);
      }
      if(st.empty())break;
    }
  }
  cout<<cnt<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int a[maxn];
map<pair<int,int>,int> mp;
int32_t main()
{
    int n,m; scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=m;i++) { int x,y; scanf("%d %d",&x,&y);  mp[{x,y}]++;}
    int t=0;
    int i=n;
    for(int j=n-1;j>=1;j--)
    {
        int x=0;
        for(int k=j+1;k<=i;k++)
        {
            if(mp[{a[j],a[k]}]) continue;
            else { x=1; break; }
        }
        if(x==0)
        {
            t++;
            for(int k=j;k<=i;k++)
            {
                swap(a[k],a[k+1]);
            }
            i--;
        }
    }
    printf("%d\n",t);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int a[maxn];
map<pair<int,int>,int> mp;
int32_t main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,m; cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) { int x,y; cin>>x>>y;  mp[{x,y}]++;}
    int t=0;
    for(int i=n;i>=2;i--)
    {
        if(mp[{a[i-1],a[i]}])
        {
            t++;
            swap(a[i],a[i-1]);
        }
        else
        {

                for(int j=i-2;j>=1;j--)
                {
                    int x=0;
                    for(int k=j+1;k<=i;k++)
                    {
                        if(mp[{a[j],a[k]}]) continue;
                        else { x=1; break; }
                    }
                    if(x==0)
                    {
                        t++;
                        for(int k=j;k<=i;k++)
                        {
                            swap(a[k],a[k+1]);
                        }
                        i--;
                    }
                }
                break;
        }
    } cout<<t<<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int a[maxn];
map<pair<int,int>,int> mp;
int32_t main()
{
    int n,m; cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) { int x,y; cin>>x>>y;  mp[{x,y}]++;}
    int t=0;
    for(int i=n;i>=2;i--)
    {
        if(mp[{a[i-1],a[i]}])
        {
            t++;
            swap(a[i],a[i-1]);
        }
        else
        {

                for(int j=i-2;j>=1;j--)
                {
                    int x=0;
                    for(int k=j+1;k<=i;k++)
                    {
                        if(mp[{a[j],a[k]}]) continue;
                        else { x=1; break; }
                    }
                    if(x==0)
                    {
                        t++;
                        for(int k=j;k<=i;k++)
                        {
                            swap(a[k],a[k+1]);
                        }
                        i--;
                    }
                }
                break;
        }
    } cout<<t<<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn];
map<pair<int,int>,int> mp;
int32_t main()
{
    int n,m; cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) { int x,y; cin>>x>>y;  mp[{x,y}]++;}
    int t=0;
    for(int i=n;i>=2;i--)
    {
        if(mp[{a[i-1],a[i]}])
        {
            t++;
            swap(a[i],a[i-1]);
        }
        else
        {

                for(int j=i-2;j>=1;j--)
                {
                    int x=0;
                    for(int k=j+1;k<=i;k++)
                    {
                        if(mp[{a[j],a[k]}]) continue;
                        else {x=1; break; }

                    }
                    if(x==0) {t++; }
                }
                break;
        }
    } cout<<t<<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn];
map<pair<int,int>,int> mp;
int32_t main()
{
    int n,m; cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) { int x,y; cin>>x>>y;  mp[{x,y}]++;}
    int t=0;
    for(int i=n;i>=2;i--)
    {
        if(mp[{a[i-1],a[i]}])
        {
            t++;
            swap(a[i],a[i-1]);
        }
        else
        {

                for(int j=i-2;j>=1;j--)
                {
                    int x=0;
                    for(int k=j+1;k<=i;k++)
                    {
                        if(mp[{a[j],a[k]}]) continue;
                        else {x=1; break; }
                    }
                    if(x==0)
                    {
                        t++;
                        for(int k=j;k<=i;k++)
                        {
                            swap(a[k],a[k+1]);
                        }
                        i--;
                    }
                }
                break;
        }
    } cout<<t<<endl;

}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int a[maxn];
map<pair<int,int>,int> mp;
int32_t main()
{
    int n,m; scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++) { int x,y; scanf("%d %d",&x,&y);  mp[{x,y}]++;}
    int t=0;
    int i=n;
    for(int j=n-1;j>=1;j--)
    {
        int x=0;
        for(int k=j+1;k<=i;k++)
        {
            if(mp[{a[j],a[k]}]) continue;
            else { x=1; break; }
        }
        if(x==0)
        {
            t++;
            for(int k=j;k<=i;k++)
            {
                swap(a[k],a[k+1]);
            }
            i--;
        }
    }
    printf("%d\n",t);
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;

signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    vector <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[u].emplace(v);
        if (v == p[n - 1])
            fl.pb(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    sort(rall(fl), [&] (int i, int j) {return rev[i] < rev[j];});
    int j = 0, ans = 0;
    vector <bool> used(fl.size());
    for (int i = n - 2; i >= 0 && j < fl.size(); --i)
    {
        if (fl[j] == p[i] && !used[j])
        {
            ++ans;
            ++j;
        }
        else
        {
        for (int lol = j; lol < fl.size(); ++lol)
        {
            if (!g[fl[lol]].count(p[i]))
                used[fl[lol]] = true;
        }
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;
signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    set <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[v].emplace(u);
        if (v == p[n - 1])
            fl.emplace(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    int ans = 0;
    vector <bool> used(n);
    for (int i = n - 2; i >= 0 && fl.size(); --i)
    {
        if (fl.count(p[i]))
        {
            ++ans;
            fl.erase(fl.find(p[i]));
        }
        else
        {
            set <int> tfl;
            for (auto &to : g[p[i]])
                if (fl.count(to))
                    tfl.emplace(to);
        fl = tfl;
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;
bool kek(int i, int j)
{
    return rev[i] > rev[j];
}
signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    set <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[v].emplace(u);
        if (v == p[n - 1])
            fl.emplace(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    int ans = 0;
    vector <bool> used(n);
    for (int i = n - 2; i >= 0 && fl.size(); --i)
    {
        auto j = fl.begin();
        int jj = *j;
        if (jj == p[i] && !used[jj])
        {
            ++ans;
            fl.erase(fl.begin());
        }
        else
        {
            if (jj == p[i] && used[jj])
                fl.erase(fl.begin());
            set <int> tfl;
            for (auto &to : g[p[i]])
                if (fl.count(to))
                    tfl.emplace(to);
        fl = tfl;
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;

signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    vector <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[u].emplace(v);
        if (v == p[n - 1])
            fl.pb(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    sort(rall(fl), [&] (int i, int j) {return rev[i] < rev[j];});
    int j = 0, ans = 0;
    vector <bool> used(fl.size());
    for (int i = n - 2; i >= 0 && j < fl.size(); --i)
    {
        if (fl[j] == p[i] && !used[j])
        {
            ++ans;
            ++j;
        }
        else
        {
            if (fl[j] == p[i] && used[j])
                ++j;
        for (int lol = 0; lol < fl.size(); ++lol)
        {
            if (!g[fl[lol]].count(p[i]))
                used[lol] = true;
        }
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;

signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    vector <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[u].emplace(v);
        if (v == p[n - 1])
            fl.pb(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    sort(rall(fl), [&] (int i, int j) {return rev[i] < rev[j];});
    int j = 0, ans = 0;
    vector <bool> used(fl.size());
    for (int i = n - 2; i >= 0 && j < fl.size(); --i)
    {
        if (fl[j] == p[i] && !used[j])
        {
            ++ans;
            ++j;
        }
        else if (fl[j] == p[i] && used[j])
            ++j;
        else
        {
        for (int lol = 0; lol < fl.size(); ++lol)
        {
            if (!g[fl[lol]].count(p[i]))
                used[fl[lol]] = true;
        }
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;

signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    vector <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[u].emplace(v);
        if (v == p[n - 1])
            fl.pb(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    sort(rall(fl), [&] (int i, int j) {return rev[i] < rev[j];});
    int j = 0, ans = 0;
    vector <bool> used(fl.size());
    for (int i = n - 2; i >= 0 && j < fl.size(); --i)
    {
        if (fl[j] == p[i] && !used[j])
        {
            ++ans;
            ++j;
        }
        else
        {
            if (fl[j] == p[i] && used[j])
                ++j;
        for (int lol = 0; lol < fl.size(); ++lol)
        {
            if (!g[fl[lol]].count(p[i]))
                used[fl[lol]] = true;
        }
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;

signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    vector <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[u].emplace(v);
        if (v == p[n - 1])
            fl.pb(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    sort(rall(fl), [&] (int i, int j) {return rev[i] < rev[j];});
    int j = 0, ans = 0;
    vector <bool> used(fl.size());
    for (int i = n - 2; i >= 0 && j < fl.size(); --i)
    {
        if (fl[j] == p[i] && !used[j])
        {
            ++ans;
            ++j;
        }
        else if (fl[j] == p[i] && used[j])
            ++j;
        else
        {
        for (int lol = j; lol < fl.size(); ++lol)
        {
            if (!g[fl[lol]].count(p[i]))
                used[fl[lol]] = true;
        }
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = double;
using pld = pair <ld, ld>;

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

int n, m;
vector <int> p, rev;
vector <set <int>> g;
vector <int> ans;
bool kek(int i, int j)
{
    return rev[i] > rev[j];
}
signed main()
{
    Start();
    cin >> n >> m;
    p.resize(n);
    cin >> p;
    for (auto &i : p)
        --i;
    g.resize(n);
    set <int> fl;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v, --u, --v;
        g[u].emplace(v);
        if (v == p[n - 1])
            fl.emplace(u);
    }
    rev.resize(n);
    for (int i = 0; i < n; ++i)
        rev[p[i]] = i;
    int ans = 0;
    vector <bool> used(n);
    for (int i = n - 2; i >= 0 && fl.size(); --i)
    {
        auto j = fl.begin();
        int jj = *j;
        if (jj == p[i] && !used[jj])
        {
            ++ans;
            fl.erase(fl.begin());
        }
        else
        {
            if (jj == p[i] && used[jj])
                fl.erase(fl.begin());
        for (auto kek = fl.begin(); kek != fl.end(); ++kek)
        {
            if (!g[*kek].count(p[i]))
                used[*kek] = true, fl.erase(kek), kek--;
        }
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
        }
        ll ans = 0, pos = n-1;
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            ll j = vec[i].sec+1;
            while( j < s(perm) -1){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                j++;
            }
            if(cont){
                ans++;
                if(vec[i].sec < s(perm)) perm.erase(perm.begin()+vec[i].sec);
                // rep(j, s(perm)) cout<<perm[j]<<" ";
                // cout<<endl;
            }
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
        }
        ll ans = 0, pos = n-1;
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            foi(j, vec[i].sec+1, pos){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
            }
            if(cont)    {ans++;perm[vec[i].sec] = perm[n-1];}
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        map<pll, ll> edges;
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
            edges[mp(a, b)] = 1;
        }
        ll ans = 0, pos = n-1;
        vl func(n), inv(n);
        rep(i, n){
            func[i] = i+1;
            inv[i] = i-1;
        }
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            ll j = vec[i].sec+1;
            while(j < pos && j < s(perm)){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                j++;
            }
            if(cont){
                ans++;
                perm.erase(perm.begin()+vec[i].sec);
                pos--;
            }
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
        }
        ll ans = 0, pos = n-1;
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            foi(j, vec[i].sec+1, pos){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
            }
            if(cont)    {ans++;perm[vec[i].sec] = perm[n-1];}
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
        }
        ll ans = 0, pos = n-1;
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            ll j = vec[i].sec+1;
            while( j < s(perm) -1){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                j++;
            }
            if(cont){
                ans++;
                perm.erase(perm.begin()+vec[i].sec);
            }
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        map<pll, ll> edges;
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
            edges[mp(a, b)] = 1;
        }
        ll ans = 0, pos = n-1;
        vl func(n), inv(n);
        rep(i, n){
            func[i] = i+1;
            inv[i] = i-1;
        }
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            ll j = vec[i].sec+1;
            while(j < pos){
                // if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                if(edges[mp(vec[i].first, perm[j])] == 0)   {cont = 0; break;}
                // j = func[j];
                j++;
            }
            if(cont){
                ans++;
                perm.erase(perm.begin()+vec[i].sec);
                pos--;
            }
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        map<pll, ll> edges;
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            graph[a].insert(b);
            if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
            edges[mp(a, b)] = 1;
        }
        ll ans = 0, pos = n-1;
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            // cout<<vec[i].first<<" "<<vec[i].sec<<endl;
            bool cont = 1;
            foi(j, vec[i].sec+1, pos){
                // if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                if(edges[mp(vec[i].first, perm[j])] == 0)   {cont = 0; break;}
            }
            if(cont)    {ans++;perm[vec[i].sec] = perm[n-1];}
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            if(graph[a].find(b) == graph[a].end()) {
                graph[a].insert(b);
                if(b == perm[n-1])  vec.pb(mp(a, idx[a]));
            }
        }
        ll ans = 0, pos = n-1;
        sort(vec.rbegin(), vec.rend(), sortbysec);
        rep(i, s(vec)){
            bool cont = 1;
            ll j = vec[i].sec+1;
            while( j < s(perm) -1){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                j++;
            }
            if(cont){
                ans++;
                if(vec[i].sec < s(perm)) perm.erase(perm.begin()+vec[i].sec);
            }
        }
        cout<<ans;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define mll map<ll, ll>
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()

using namespace std;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
bool sortbysec(const pll &a, const pll &b){return (a.second < b.second);}
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("../input.txt", "r", stdin);
        freopen("../output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll n, m;
        cin>>n>>m;
        vl perm(n), idx(n+1);
        vector<set<ll> > graph(n+1);
        vlp vec;
        rep(i, n)   {cin>>perm[i];idx[perm[i]] = i;}
        ll a, b;
        rep(i, m){
            cin>>a>>b;
            if(graph[a].find(b) == graph[a].end()) {graph[a].insert(b);
                        if(b == perm[n-1])  vec.pb(mp(a, idx[a]));}
        }
        ll ans = 0, pos = n-1;
        sort(vec.begin(), vec.end(), sortbysec);
        fod(i, s(vec)-1, 0){
            bool cont = 1;
            ll j = vec[i].sec+1;
            while( j < s(perm) -1){
                if(graph[vec[i].first].find(perm[j]) == graph[vec[i].first].end())  {cont = 0;break;}
                j++;
            }
            if(cont){
                ans++;
                if(vec[i].sec < s(perm)) perm.erase(perm.begin()+vec[i].sec);
            }
        }
        cout<<ans;
    }
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 300005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        cout << g[i].size() << "\n";
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        cout << n - 1 - ii - ans << "\n";
        if (cnt == n - 1 - ii - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (i == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == i - n + 1 - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << n - ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == i - n + 1 - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << n - ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 300005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == n - 1 - ii - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (i == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == i - n + 1 - ans) {
            ++ans;
            used[j] = 1;
        }
    }
    cout << n - ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> vec[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        vec[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (i == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == i - n + 1 - ans) {
            ++ans;
            used[j] = 1;
        }
    }
    cout << n - ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == n - 1 - ii - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << n - ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == n - 1 - ii - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << n - ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == n - 1 - ii - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <random>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>
#include <cassert>
#include <complex>

using namespace std;

#define mp make_pair
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define all_(x) x.rbegin(), x.rend()
#define multi_test 0

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
typedef complex<ld> base;

const int INF = 2e9 + 9;
const ll INF1 = 8e18 + 9;
const ll MAXN = 100005;
const ll MAXN1 = 1 << 15;
const ll MAXN2 = 55 * MAXN;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll ALPH = 27;
const ll PW1 = 2;
const ll PW2 = 199;
const ll PW3 = 193;
const ll PW4 = 117;
const ld EPS = 1e-7;
const ll BLOCK = 316;
const ll BLOCK1 = 1 << 9;
const ull T = 3e9 + 239;

void solve();

signed main(){
	srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif // _DEBUG
	int q = 1;
	if (multi_test) cin >> q;
	while (q--) solve();
}

/*------------------------------------------------------------------------------------------------------------*/

vector<int> g[MAXN];
bool used[MAXN], used1[MAXN];
int v[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
    }
    int ans = 0;
    for (int ii = n - 1; ii >= 0; --ii) {
        int i = v[ii];
        used1[i] = 1;
        if (ii == n - 1) continue;
        int cnt = 0;
        for (int &j : g[i]) {
            if (used1[j] && !used[j]) ++cnt;
        }
        if (cnt == n - 1 - i - ans) {
            ++ans;
            used[i] = 1;
        }
    }
    cout << n - ans;
}


#include <bits/stdc++.h>
#define  ld long double
#define  nt int64_t
#define  st string
#define  sc second
#define  fr first
#define  rn return 0;
#define  Aziz for(nt i = 1; i <= n; i++)
#define  Azi for(nt i = 1; i < n; i++)
#define  Az for(nt i = 0; i <= n; i++)
#define  A for(nt i = 0; i < n; i++)
#define  Ziz for(nt j = 1; j <= n; j++)
#define  Zi for(nt j = 1; j < n; j++)
#define  Iz for(nt j = 0; j <= n; j++)
#define  Z for(nt j = 0; j < n; j++)
#define  io ios_base::sync_with_stdio(0);
using namespace std;
nt n, m;
deque <nt> d[20001];
nt p[20001];
bool mark[20001];
nt ans;
int main()
{
    io
    cin >> n >> m;
    Aziz
    {
        nt x;
        cin >> x;
        p[x] = i;
    }
    for(nt i = 1; i <= m; i++)
    {
        nt x, y;
        cin >> x >> y;
        d[p[x]].push_back(p[y]);
    }
    mark[n] = true;
    for(nt i = n - 1; i >= 1; i--)
    {
        nt c = 0;
        for(nt j = 0; j < d[i].size(); j++)
        {
            if(mark[d[i][j]])
                c++;
        }
        if(c + ans < n - i)
            mark[i] = true;
        else
            ans++;
    }
    cout << ans;
    rn
}


#include <bits/stdc++.h>
#define  ld long double
#define  nt int64_t
#define  st string
#define  sc second
#define  fr first
#define  rn return 0;
#define  Aziz for(nt i = 1; i <= n; i++)
#define  Azi for(nt i = 1; i < n; i++)
#define  Az for(nt i = 0; i <= n; i++)
#define  A for(nt i = 0; i < n; i++)
#define  Ziz for(nt j = 1; j <= n; j++)
#define  Zi for(nt j = 1; j < n; j++)
#define  Iz for(nt j = 0; j <= n; j++)
#define  Z for(nt j = 0; j < n; j++)
#define  io ios_base::sync_with_stdio(0);
using namespace std;
nt n, m;
deque <nt> d[2000001];
nt p[2000001];
bool mark[2000001];
nt ans;
int main()
{
    io
    cin >> n >> m;
    Aziz
    {
        nt x;
        cin >> x;
        p[x] = i;
    }
    for(nt i = 1; i <= m; i++)
    {
        nt x, y;
        cin >> x >> y;
        d[p[x]].push_back(p[y]);
    }
    mark[n] = true;
    for(nt i = n - 1; i >= 1; i--)
    {
        nt c = 0;
        for(nt j = 0; j < d[i].size(); j++)
        {
            if(mark[d[i][j]])
                c++;
        }
        if(c + ans < n - i)
            mark[i] = true;
        else
            ans++;
    }
    cout << ans;
    rn
}


#include <bits/stdc++.h>
#define  ld long double
#define  nt int64_t
#define  st string
#define  sc second
#define  fr first
#define  rn return 0;
#define  Aziz for(nt i = 1; i <= n; i++)
#define  Azi for(nt i = 1; i < n; i++)
#define  Az for(nt i = 0; i <= n; i++)
#define  A for(nt i = 0; i < n; i++)
#define  Ziz for(nt j = 1; j <= n; j++)
#define  Zi for(nt j = 1; j < n; j++)
#define  Iz for(nt j = 0; j <= n; j++)
#define  Z for(nt j = 0; j < n; j++)
#define  io ios_base::sync_with_stdio(0);
using namespace std;
nt n, m;
deque <nt> d[500001];
nt p[500001];
bool mark[500001];
nt ans;
int main()
{
    io
    cin >> n >> m;
    Aziz
    {
        nt x;
        cin >> x;
        p[x] = i;
    }
    for(nt i = 1; i <= m; i++)
    {
        nt x, y;
        cin >> x >> y;
        d[p[x]].push_back(p[y]);
    }
    mark[n] = true;
    for(nt i = n - 1; i >= 1; i--)
    {
        nt c = 0;
        for(nt j = 0; j < d[i].size(); j++)
        {
            if(mark[d[i][j]])
                c++;
        }
        if(c + ans < n - i)
            mark[i] = true;
        else
            ans++;
    }
    cout << ans;
    rn
}


#include <bits/stdc++.h>
#define  ld long double
#define  nt int64_t
#define  st string
#define  sc second
#define  fr first
#define  rn return 0;
#define  Aziz for(nt i = 1; i <= n; i++)
#define  Azi for(nt i = 1; i < n; i++)
#define  Az for(nt i = 0; i <= n; i++)
#define  A for(nt i = 0; i < n; i++)
#define  Ziz for(nt j = 1; j <= n; j++)
#define  Zi for(nt j = 1; j < n; j++)
#define  Iz for(nt j = 0; j <= n; j++)
#define  Z for(nt j = 0; j < n; j++)
#define  io ios_base::sync_with_stdio(0);
using namespace std;
nt n, m;
deque <nt> d[200001];
nt p[200001];
bool mark[200001];
nt ans;
int main()
{
    io
    cin >> n >> m;
    Aziz
    {
        nt x;
        cin >> x;
        p[x] = i;
    }
    for(nt i = 1; i <= m; i++)
    {
        nt x, y;
        cin >> x >> y;
        d[p[x]].push_back(p[y]);
    }
    mark[n] = true;
    for(nt i = n - 1; i >= 1; i--)
    {
        nt c = 0;
        for(nt j = 0; j < d[i].size(); j++)
        {
            if(mark[d[i][j]])
                c++;
        }
        if(c + ans < n - i)
            mark[i] = true;
        else
            ans++;
    }
    cout << ans;
    rn
}


#include <bits/stdc++.h>
#define  ld long double
#define  nt int64_t
#define  st string
#define  sc second
#define  fr first
#define  rn return 0;
#define  Aziz for(nt i = 1; i <= n; i++)
#define  Azi for(nt i = 1; i < n; i++)
#define  Az for(nt i = 0; i <= n; i++)
#define  A for(nt i = 0; i < n; i++)
#define  Ziz for(nt j = 1; j <= n; j++)
#define  Zi for(nt j = 1; j < n; j++)
#define  Iz for(nt j = 0; j <= n; j++)
#define  Z for(nt j = 0; j < n; j++)
#define  io ios_base::sync_with_stdio(0);
using namespace std;
nt n, m;
deque <nt> d[300001];
nt p[300001];
bool mark[300001];
nt ans;
int main()
{
    io
    cin >> n >> m;
    Aziz
    {
        nt x;
        cin >> x;
        p[x] = i;
    }
    for(nt i = 1; i <= m; i++)
    {
        nt x, y;
        cin >> x >> y;
        d[p[x]].push_back(p[y]);
    }
    mark[n] = true;
    for(nt i = n - 1; i >= 1; i--)
    {
        nt c = 0;
        for(nt j = 0; j < d[i].size(); j++)
        {
            if(mark[d[i][j]])
                c++;
        }
        if(c + ans < n - i)
            mark[i] = true;
        else
            ans++;
    }
    cout << ans;
    rn
}