#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> soproc;
vector<int> cnt;
vector<bool> was;
void dfs(int v, set<int>& to) {
  for (int u : g[v]) {
    cnt[u]--;
    if (!cnt[u])
      if (soproc[u] != soproc[v])
        to.insert(u);
      else
        dfs(u, to);
  }
}
void clearall(set<int>& now, set<int>& to) {
  for (int v : now) dfs(v, to);
  now.clear();
}
int main() {
  int n, m;
  cin >> n >> m;
  g.resize(n);
  soproc.resize(n);
  cnt.resize(n);
  was.resize(n);
  for (int i = 0; i < n; i++) cin >> soproc[i];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    cnt[v]++;
  }
  set<int> pr, sp;
  for (int i = 0; i < n; i++)
    if (!soproc[i] && !cnt[i]) pr.insert(i);
  clearall(pr, sp);
  for (int i = 0; i < n; i++)
    if (soproc[i] && cnt[i] == 0) sp.insert(i);
  int res = 0;
  while (!sp.empty()) {
    res++;
    clearall(sp, pr);
    clearall(pr, sp);
    res += 0;
  }
  for (int i = 0; i < n; i++)
    if (soproc[i] && cnt[i])
      while (true)
        ;
  cout << res;
}
