#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int n, m;
int p[500010], pos[500010], dp[500010];
bool viz[500010];
vector<int> g[500010];
int memo(int node) {
  if (viz[node]) return dp[node];
  viz[node] = true;
  for (int i = 0; i < ((int)(g[node].size())); i++)
    dp[node] = max(dp[node], memo(g[node][i]) + 1);
  return dp[node];
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    pos[p[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x = pos[x];
    y = pos[y];
    g[y].push_back(x);
  }
  printf("%d", memo(n));
  return 0;
}
