#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  int i, j, k, n, m;
  cin >> n >> m;
  vector<int> g[n], vis(n), deg(n), type(n);
  for (i = 0; i < n; ++i) cin >> type[i];
  for (i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    deg[v]++;
  }
  queue<int> C, M;
  for (i = 0; i < n; ++i) {
    if (deg[i] == 0) {
      if (type[i])
        C.push(i);
      else
        M.push(i);
    }
  }
  int ans = 0;
  while (!C.empty() or !M.empty()) {
    while (!M.empty()) {
      int u = M.front();
      M.pop();
      for (auto v : g[u]) {
        deg[v]--;
        if (deg[v] == 0) {
          if (type[v])
            C.push(v);
          else
            M.push(v);
        }
      }
    }
    if (!C.empty()) {
      ans++;
      while (!C.empty()) {
        int u = C.front();
        C.pop();
        for (auto v : g[u]) {
          deg[v]--;
          if (deg[v] == 0) {
            if (type[v])
              C.push(v);
            else
              M.push(v);
          }
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
