#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  scanf("%i %i", &n, &m);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    scanf("%i", &a[i]);
  }
  vector<int> indeg(n);
  vector<vector<int>> t(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%i %i", &u, &v);
    t[v].push_back(u);
    indeg[u]++;
  }
  deque<int> Q;
  for (int i = 0; i < n; i++) {
    if (!indeg[i]) {
      if (a[i]) {
        Q.push_back(i);
      } else {
        Q.push_front(i);
      }
    }
  }
  int ans = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop_front();
    if (a[cur]) {
      ans++;
      vector<int> tmp;
      Q.push_front(cur);
      while (!Q.empty()) {
        int u = Q.front();
        Q.pop_front();
        assert(a[u]);
        for (int v : t[u]) {
          indeg[v]--;
          if (!indeg[v]) {
            if (a[v]) {
              Q.push_back(v);
            } else {
              tmp.push_back(v);
            }
          }
        }
      }
      while (!tmp.empty()) {
        Q.push_back(tmp.back());
        tmp.pop_back();
      }
    } else {
      for (int v : t[cur]) {
        indeg[v]--;
        if (!indeg[v]) {
          if (a[v]) {
            Q.push_back(v);
          } else {
            Q.push_front(v);
          }
        }
      }
    }
  }
  printf("%i\n", ans);
  return 0;
}
