#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, tot, u, v, ans;
int e[N];
int head[N], ver[N], last[N], deg[N];
queue<int> qa, qm;
void add(int x, int y) {
  ver[++tot] = y;
  last[tot] = head[x];
  head[x] = tot;
}
void topo() {
  for (int i = 1; i <= n; i++) {
    if (!deg[i]) {
      if (e[i])
        qa.push(i);
      else
        qm.push(i);
    }
  }
  while (qa.size() || qm.size()) {
    while (qm.size()) {
      int x = qm.front();
      qm.pop();
      for (int i = head[x]; i; i = last[i]) {
        int y = ver[i];
        deg[y]--;
        if (!deg[y]) {
          if (e[y])
            qa.push(y);
          else
            qm.push(y);
        }
      }
    }
    if (qa.size()) ans++;
    while (qa.size()) {
      int x = qa.front();
      qa.pop();
      for (int i = head[x]; i; i = last[i]) {
        int y = ver[i];
        deg[y]--;
        if (!deg[y]) {
          if (e[y])
            qa.push(y);
          else
            qm.push(y);
        }
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> e[i];
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    u++;
    v++;
    add(v, u);
    deg[u]++;
  }
  topo();
  cout << ans;
  return 0;
}
