#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3 * 100000;
set<int> can_through[MAX_N + 1];
vector<int> query;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    query.emplace_back(a);
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    can_through[a].insert(b);
  }
  int dp[n];
  for (int i = 0; i < n; ++i) {
    dp[i] = 0;
  }
  for (int i = 1; i < n; ++i) {
    int deepth = 1 + dp[i - 1];
    for (int j = 0; j <= deepth && (i - j >= 0); ++j) {
      if (can_through[query[i - j]].count(query[i])) {
        dp[i]++;
        deepth++;
      }
    }
  }
  cout << dp[n - 1];
}
