#include <bits/stdc++.h>
using namespace std;
int Int() {
  int x;
  scanf("%d", &x);
  return x;
}
long long Long() {
  long long x;
  scanf("%lld", &x);
  return x;
}
double Double() {
  double x;
  scanf("%lf", &x);
  return x;
}
const int N = 3 * (int)1e5 + 5;
const long long MOD = (int)1e9 + 7;
int arr[N];
int ans[N];
vector<int> con;
int cng[N];
int pos[N];
vector<int> g[N];
int main() {
  int n = Int(), m = Int();
  for (int i = 0; i < n; i++) {
    arr[i] = Int();
    pos[arr[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    int u = Int(), v = Int();
    g[u].push_back(v);
    g[v].push_back(u);
    if (u == arr[n - 1]) ans[v] = 1;
    if (v == arr[n - 1]) ans[u] = 1;
    if (abs(pos[u] - pos[v]) == 1) {
      cng[u] = cng[v] = 1;
    }
  }
  int anss = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (cng[arr[i]]) {
      con.push_back(arr[i]);
      con.push_back(arr[i - 1]);
      int j;
      for (j = i + 1; j < n; j++) {
        bool fnd = false;
        for (auto k : con) {
          vector<int>::iterator it =
              find(g[arr[j]].begin(), g[arr[j]].end(), k);
          if (it != g[arr[j]].end()) {
            cng[arr[j]] = 1;
            con.push_back(arr[j]);
            fnd = true;
            break;
          }
        }
        if (!fnd) break;
      }
      if (j < n - 1) return puts("0"), 0;
      for (j = i - 2; j >= 0; j--) {
        bool fnd = false;
        for (auto k : con) {
          vector<int>::iterator it =
              find(g[arr[j]].begin(), g[arr[j]].end(), k);
          if (it != g[arr[j]].end()) {
            cng[arr[j]] = 1;
            con.push_back(arr[j]);
            fnd = true;
            if (ans[arr[j]] == 1) anss++;
            break;
          }
        }
        if (!fnd) break;
      }
      if (ans[arr[i]]) anss++;
      if (ans[arr[i - 1]]) anss++;
      return cout << anss << '\n', 0;
    }
  }
  return 0;
}
