#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
using ll = int64_t;
using Vi = vector<int>;
using Pii = pair<int, int>;
void debx(...) {}
template <class T, class U = int, class... V>
void debx(T s, U a, V... b) {
  while (*s && *s != ',') cerr << *s++;
  cerr << ": " << a << *s++;
  debx(s, b...);
}
template <class T, class U = int, class... V>
auto operator<<(ostream& o, T x) -> decltype(&T::print, cout) {
  x.print();
  return o;
}
template <class T, class U = int, class... V>
auto operator<<(ostream& o, pair<T, U> x) -> decltype(0, cout) {
  return o << "(" << x.first << ", " << x.second << ")";
}
template <class T, class U = int, class... V>
auto operator<<(ostream& o, T x) -> decltype(T().begin(), cout) {
  o << "[";
  for (auto& e : (x)) o << e << ", ";
  return o << "]";
}
struct Vert {
  Vi edges, invEdges;
  int coproc, out{0};
};
int V, E;
vector<Vert> verts;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> V >> E;
  verts.resize(V);
  for (auto& v : (verts)) cin >> v.coproc;
  for (int i = (0); i < (E); i++) {
    int a, b;
    cin >> a >> b;
    verts[a].edges.push_back(b);
    verts[b].invEdges.push_back(a);
    verts[a].out++;
  }
  queue<int> avail, coproc;
  for (int i = (0); i < (V); i++)
    if (verts[i].out == 0) {
      if (verts[i].coproc)
        coproc.push(i);
      else
        avail.push(i);
    }
  int ret = 0;
  while (true) {
    while (!avail.empty()) {
      int v = avail.front();
      avail.pop();
      for (auto& e : (verts[v].invEdges))
        if (--verts[e].out == 0) {
          if (verts[e].coproc)
            coproc.push(e);
          else
            avail.push(e);
        }
    }
    if (coproc.empty()) break;
    ret++;
    while (!coproc.empty()) {
      int v = coproc.front();
      coproc.pop();
      for (auto& e : (verts[v].invEdges))
        if (--verts[e].out == 0) {
          if (verts[e].coproc)
            coproc.push(e);
          else
            avail.push(e);
        }
    }
  }
  cout << ret << endl;
  return 0;
}
