#include <bits/stdc++.h>
using namespace std;
const int maxn = 1.1e5;
queue<int> Q[2];
vector<int> E[maxn];
int n, m, cnt, in[maxn], f[maxn];
signed main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", f + i);
  for (int i = 1, x, y; i <= m; ++i)
    scanf("%d%d", &x, &y), ++in[x], E[y].push_back(x);
  for (int i = 0; i < n; ++i)
    if (!in[i]) Q[f[i]].push(i);
  while (!(Q[1].empty() && Q[0].empty())) {
    while (!Q[0].empty()) {
      int u = Q[0].front();
      Q[0].pop();
      for (auto v : E[u])
        if (!--in[v]) Q[f[v]].push(v);
    }
    if (!Q[1].empty()) ++cnt;
    while (!Q[1].empty()) {
      int u = Q[1].front();
      Q[1].pop();
      for (auto v : E[u])
        if (!--in[v]) Q[f[v]].push(v);
    }
  }
  printf("%d", cnt);
  return 0;
}
