#include <bits/stdc++.h>
using namespace std;
int type[100004];
set<int> adj[100004];
int income[100004];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> type[i];
    type[i] = (type[i] + 1) % 2;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[y].insert(x);
    income[x]++;
  }
  vector<int> t0, t1;
  for (int i = 0; i < n; i++) {
    if (income[i] == 0 && type[i] == 0) t0.push_back(i);
    if (income[i] == 0 && type[i] == 1) t1.push_back(i);
  }
  int done = 0;
  int ans = 0;
  while (done < n) {
    for (int i = 0; i < t1.size(); i++) {
      for (int nxt : adj[t1[i]]) {
        income[nxt]--;
        if (income[nxt] == 0 && type[nxt] == 1) t1.push_back(nxt);
        if (income[nxt] == 0 && type[nxt] == 0) t0.push_back(nxt);
      }
    }
    done += t1.size();
    t1 = vector<int>();
    for (int i = 0; i < t0.size(); i++) {
      for (int nxt : adj[t0[i]]) {
        income[nxt]--;
        if (income[nxt] == 0 && type[nxt] == 0) t0.push_back(nxt);
        if (income[nxt] == 0 && type[nxt] == 1) t1.push_back(nxt);
      }
    }
    if (!t0.empty()) ans++;
    done += t0.size();
    t0 = vector<int>();
  }
  cout << ans;
  return 0;
}
