#include <bits/stdc++.h>
using namespace std;
struct hs {
  int b, id;
};
struct hs1 {
  int l, r;
};
hs d[500005];
hs1 f[500005];
int n, m, i, a[500005], l[500005], r[500005], c[500005];
bool ss(hs &x, hs &y) { return (x.b < y.b); }
bool ss1(hs1 &x, hs1 &y) { return (x.l < y.l || (x.l == y.l && x.r > y.r)); }
bool ss2(hs1 &x, hs1 &y) { return (x.l < y.l || (x.l == y.l && x.r < y.r)); }
int thayso(int x) {
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (x < d[mid].b) {
      r = mid - 1;
    } else if (x > d[mid].b) {
      l = mid + 1;
    } else if (x == d[mid].b) {
      return d[mid].id;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    d[i].b = a[i];
    d[i].id = i;
  }
  sort(d + 1, d + n + 1, ss);
  for (i = 1; i <= n; i++) {
    c[i] = i;
    a[i] = i;
  }
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &f[i].l, &f[i].r);
    f[i].l = thayso(f[i].l);
    f[i].r = thayso(f[i].r);
  }
  int ans = 0;
  sort(f + 1, f + m + 1, ss1);
  for (i = 1; i <= m; i++) {
    if (a[c[f[i].l]] == f[i].l && a[c[f[i].l] + 1] == f[i].r) {
      swap(a[c[f[i].l]], a[c[f[i].l] + 1]);
      swap(c[f[i].l], c[f[i].r]);
    }
  }
  for (i = 1; i <= n; i++)
    if (a[i] == n) {
      ans = max(ans, n - i);
      break;
    }
  for (i = 1; i <= n; i++) {
    c[i] = i;
    a[i] = i;
  }
  sort(f + 1, f + m + 1, ss2);
  for (i = 1; i <= m; i++) {
    if (a[c[f[i].l]] == f[i].l && a[c[f[i].l] + 1] == f[i].r) {
      swap(a[c[f[i].l]], a[c[f[i].l] + 1]);
      swap(c[f[i].l], c[f[i].r]);
    }
  }
  for (i = 1; i <= n; i++)
    if (a[i] == n) {
      ans = max(ans, n - i);
      break;
    }
  for (i = 1; i <= n; i++) {
    c[i] = i;
    a[i] = i;
  }
  sort(f + 1, f + m + 1, ss1);
  for (i = m; i >= 1; i--) {
    if (a[c[f[i].l]] == f[i].l && a[c[f[i].l] + 1] == f[i].r) {
      swap(a[c[f[i].l]], a[c[f[i].l] + 1]);
      swap(c[f[i].l], c[f[i].r]);
    }
  }
  for (i = 1; i <= n; i++)
    if (a[i] == n) {
      ans = max(ans, n - i);
      break;
    }
  for (i = 1; i <= n; i++) {
    c[i] = i;
    a[i] = i;
  }
  sort(f + 1, f + m + 1, ss2);
  for (i = m; i >= 1; i--) {
    if (a[c[f[i].l]] == f[i].l && a[c[f[i].l] + 1] == f[i].r) {
      swap(a[c[f[i].l]], a[c[f[i].l] + 1]);
      swap(c[f[i].l], c[f[i].r]);
    }
  }
  for (i = 1; i <= n; i++)
    if (a[i] == n) {
      ans = max(ans, n - i);
      break;
    }
  printf("%d", ans);
}
