#include <bits/stdc++.h>
using namespace std;
int n, t[(1 << 20)], cntP[(1 << 20)];
vector<int> chs[(1 << 20)];
vector<int> p[2];
int main() {
  int m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    swap(a, b);
    cntP[a]++;
    chs[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    if (cntP[i] == 0) {
      p[t[i]].push_back(i);
    }
  }
  int cntT = 0;
  for (int v = 0;; cntT++, v ^= 1) {
    if (p[0].size() == 0 && p[1].size() == 0) {
      break;
    }
    for (int i = 0; i < p[v].size(); i++) {
      for (int j = 0; j < chs[p[v][i]].size(); j++) {
        cntP[chs[p[v][i]][j]]--;
        if (cntP[chs[p[v][i]][j]] == 0) {
          p[t[chs[p[v][i]][j]]].push_back(chs[p[v][i]][j]);
        }
      }
    }
    p[v].clear();
  }
  cout << cntT / 2 << "\n";
  return 0;
}
