#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<vector<int> > g(n);
  vector<vector<int> > gr(n);
  vector<int> deg(n);
  while (m--) {
    int u, v;
    cin >> u >> v;
    g[v].push_back(u);
    gr[u].push_back(v);
    ++deg[u];
  }
  queue<int> q1;
  queue<int> q2;
  vector<bool> inQueue(n, false);
  for (int i = 0; i < n; ++i) {
    if (a[i] == 0 && deg[i] == 0) {
      q1.push(i);
      inQueue[i] = true;
    }
    if (a[i] == 1 && deg[i] == 0) {
      q2.push(i);
      inQueue[i] = true;
    }
  }
  int ans = 0;
  int done = 0;
  while (done < n) {
    while (!q1.empty()) {
      int v = q1.front();
      ++done;
      q1.pop();
      for (int to : g[v]) {
        --deg[to];
        if (deg[to] == 0) {
          if (a[to] == 0 && !inQueue[to]) {
            q1.push(to);
            inQueue[to] = true;
          }
          if (a[to] == 1 && !inQueue[to]) {
            q2.push(to);
            inQueue[to] = true;
          }
        }
      }
    }
    if (q2.empty()) {
      assert(done == n);
      break;
    }
    ++ans;
    while (!q2.empty()) {
      int v = q2.front();
      ++done;
      q2.pop();
      for (int to : g[v]) {
        --deg[to];
        if (deg[to] == 0) {
          if (a[to] == 0 && !inQueue[to]) {
            q1.push(to);
            inQueue[to] = true;
          }
          if (a[to] == 1 && !inQueue[to]) {
            q2.push(to);
            inQueue[to] = true;
          }
        }
      }
    }
  }
  cout << ans << "\n";
}
