#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int n, in[N], g[N], a[N], t[N], f[N], m, ans, cnt, M;
struct edge {
  int next, to;
} e[N];
queue<int> q[2];
inline void addedge(int x, int y) {
  e[++M] = (edge){g[x], y};
  g[x] = M;
  in[y]++;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    addedge(y + 1, x + 1);
  }
  for (int i = 1; i <= n; i++)
    if (!in[i]) q[a[i]].push(i);
  for (int k = 0;; k ^= 1) {
    while (!q[k].empty()) {
      int x = q[k].front();
      q[k].pop();
      cnt++;
      for (int i = g[x], y; i; i = e[i].next) {
        in[y = e[i].to]--;
        if (!in[y]) {
          q[a[y]].push(y);
        }
      }
    }
    ans += k;
    if (cnt == n) break;
  }
  printf("%d", ans);
  return 0;
}
