#include <bits/stdc++.h>
using namespace std;
bool e[100003];
long long int deg[100003];
vector<long long int> g[100003];
vector<long long int> rg[100003];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 0; i < n; i++) {
    cin >> e[i];
  }
  for (long long int i = 0; i < m; i++) {
    long long int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    rg[v].push_back(u);
    deg[u]++;
  }
  queue<long long int> q1;
  queue<long long int> q2;
  for (long long int i = 0; i < n; i++) {
    if (deg[i] == 0 && e[i] == 0) {
      q1.push(i);
    }
    if (deg[i] == 0 && e[i] == 1) {
      q2.push(i);
    }
  }
  while (q1.size()) {
    auto node = q1.front();
    q1.pop();
    for (auto it : rg[node]) {
      deg[it]--;
      if (deg[it] == 0) {
        if (e[it] == 0) {
          q1.push(it);
        } else {
          q2.push(it);
        }
      }
    }
  }
  long long int res = 0;
  while (q2.size()) {
    res++;
    while (q2.size()) {
      auto node = q2.front();
      q2.pop();
      for (auto it : rg[node]) {
        deg[it]--;
        if (deg[it] == 0) {
          if (e[it] == 0) {
            q1.push(it);
          } else {
            q2.push(it);
          }
        }
      }
    }
    while (q1.size()) {
      auto node = q1.front();
      q1.pop();
      for (auto it : rg[node]) {
        deg[it]--;
        if (deg[it] == 0) {
          if (e[it] == 0) {
            q1.push(it);
          } else {
            q2.push(it);
          }
        }
      }
    }
  }
  cout << res << "\n";
}
