#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int ans, tot, head[N], ver[N], Next[N], val[N], n, m, deg[N];
queue<int> q[2];
bool vis[N];
inline void add(int x, int y) {
  ver[++tot] = y;
  Next[tot] = head[x];
  head[x] = tot;
  deg[y]++;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x++;
    y++;
    add(y, x);
  }
  for (int i = 1; i <= n; i++)
    if (!deg[i]) q[val[i]].push(i);
  while ((!q[0].empty()) || (!q[1].empty())) {
    while (!q[0].empty()) {
      int x = q[0].front();
      q[0].pop();
      vis[x] = true;
      for (int i = head[x]; i; i = Next[i]) {
        int y = ver[i];
        if (vis[y]) continue;
        if (!(--deg[y])) q[val[y]].push(y);
      }
    }
    if (!q[1].empty()) {
      ans++;
      while (!q[1].empty()) {
        int x = q[1].front();
        q[1].pop();
        vis[x] = true;
        for (int i = head[x]; i; i = Next[i]) {
          int y = ver[i];
          if (vis[y]) continue;
          if (!(--deg[y])) q[val[y]].push(y);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
