#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, m, a[300000], x, y, l, lol;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] = n) l = i;
  }
  for (int j = 0; j < m; j++) {
    cin >> x >> y;
    if (x = n) {
      int k = 0;
      while (a[k] != x) {
        k = k + 1;
      }
      lol = k;
    } else if (y = n) {
      int k = 0;
      while (a[k] != y) {
        k = k + 1;
      }
      lol = k;
    }
  }
  cout << abs(lol - l);
}
