#include <bits/stdc++.h>
using namespace std;
vector<long long> p, used, temp[310000];
long long temp2[310000];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  long long n, m;
  long long u, v;
  long long ans = 0, con;
  cin >> n >> m;
  p.resize(n + 1);
  used.resize(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> u >> v;
    if (v == p[n]) used[u] = true;
    temp[u].push_back(v);
  }
  con = n - 1;
  for (long long i = n - 1; i >= 0; i--, con--) {
    if (used[p[i]] == false) {
      break;
    }
  }
  ans = n - con - 1;
  for (long long i = 1; i <= n; i++) {
    if (used[i] == false) continue;
    for (long long j = 0; j < temp[i].size(); j++) {
      temp2[temp[i][j]] = i;
    }
    long long temp3 = con;
    for (long long j = con; j >= 1; j--, temp3--) {
      if (temp2[p[j]] != i) {
        break;
      }
    }
    ans = max(ans, n - temp3 - 1);
  }
  cout << ans << "\n";
  return 0;
}
