#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
char el = '\n';
char esp = ' ';
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
ostream& operator<<(ostream& os, const vector<int>& v) {
  for (auto const& i : v) {
    os << i << " ";
  }
  os << '\n';
  return os;
}
string yes = "YES";
string no = "NO";
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m, a, b;
  cin >> n >> m;
  vector<bool> proc(n);
  vector<vi> g(n);
  vector<int> indegree(n);
  for (int i = 0; i < n; i++) {
    cin >> a;
    proc[i] = a;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[b].push_back(a);
    indegree[a]++;
  }
  int ans = 0;
  queue<int> cp;
  queue<int> mp;
  for (int i = 0; i < n; i++) {
    if (!indegree[i]) {
      if (proc[i])
        cp.push(i);
      else
        mp.push(i);
    }
  }
  while (cp.size() || mp.size()) {
    while (mp.size()) {
      int u = mp.front();
      mp.pop();
      for (int v : g[u]) {
        indegree[v]--;
        if (!indegree[v]) {
          if (proc[v])
            cp.push(v);
          else
            mp.push(v);
        }
      }
    }
    ans += !cp.empty();
    while (cp.size()) {
      int u = cp.front();
      cp.pop();
      for (int v : g[u]) {
        indegree[v]--;
        if (!indegree[v]) {
          if (proc[v])
            cp.push(v);
          else
            mp.push(v);
        }
      }
    }
  }
  cout << ans << el;
}
