#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, tot, a, b, ans = 0, e[100005], indeg[100005] = {0};
  vector<int> v[100005];
  queue<int> c, m;
  scanf("%d%d", &n, &tot);
  for (int i = 0; i < n; i++) scanf("%d", &e[i]);
  for (int i = 1; i <= tot; i++) {
    scanf("%d%d", &a, &b);
    v[b].push_back(a);
    indeg[a]++;
  }
  for (int i = 0; i < n; i++) {
    if (!indeg[i]) {
      if (e[i])
        c.push(i);
      else
        m.push(i);
    }
  }
  while (!(c.empty() && m.empty())) {
    while (!m.empty()) {
      int node = m.front();
      m.pop();
      for (auto child : v[node]) {
        indeg[child]--;
        if (!indeg[child]) {
          if (e[child])
            c.push(child);
          else
            m.push(child);
        }
      }
    }
    if (!c.empty()) {
      ans++;
      while (!c.empty()) {
        int node = c.front();
        c.pop();
        for (auto child : v[node]) {
          indeg[child]--;
          if (!indeg[child]) {
            if (e[child])
              c.push(child);
            else
              m.push(child);
          }
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
