#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
template <typename T>
T getint() {
  T val = 0;
  char c;
  while ((c = getchar()) && !(c >= '0' && c <= '9'))
    ;
  do {
    val = (val * 10) + c - '0';
  } while ((c = getchar()) && (c >= '0' && c <= '9'));
  return val;
}
int e[100501];
vector<vector<int>> adj(100501);
int indeg[100501];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> e[i];
  }
  set<pair<int, int>> st[2];
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    swap(a, b);
    adj[a].push_back(b);
    indeg[b]++;
  }
  for (int i = 0; i < n; ++i) {
    st[e[i]].insert({indeg[i], i});
  }
  int ans = 0;
  while (!st[0].empty() || !st[1].empty()) {
    while (!st[0].empty() && st[0].begin()->first == 0) {
      auto it = st[0].begin();
      auto val = *it;
      st[0].erase(it);
      for (auto i : adj[val.second]) {
        auto masik = st[e[i]].lower_bound({indeg[i], i});
        st[e[i]].erase(masik);
        indeg[i]--;
        st[e[i]].insert({indeg[i], i});
      }
    }
    if (st[0].empty() && st[1].empty()) break;
    ans++;
    while (!st[1].empty() && st[1].begin()->first == 0) {
      auto it = st[1].begin();
      auto val = *it;
      st[1].erase(it);
      for (auto i : adj[val.second]) {
        auto masik = st[e[i]].lower_bound({indeg[i], i});
        st[e[i]].erase(masik);
        indeg[i]--;
        st[e[i]].insert({indeg[i], i});
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
