#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mod = 1e9 + 7;
vector<int> g[N];
int ans, n, m, u, v, du[N], a[N];
bool ok;
queue<int> q1, q2;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (0); i < (n); i++) cin >> a[i];
  for (int i = (0); i < (m); i++) {
    cin >> u >> v;
    g[v].push_back(u);
    du[u]++;
  }
  for (int i = (0); i < (n); i++)
    if (du[i] == 0) {
      if (a[i] == 0)
        q1.push(i);
      else
        q2.push(i);
    }
  while (1) {
    while (!q1.empty()) {
      u = q1.front();
      q1.pop();
      for (int i = (0); i < ((int)g[u].size()); i++) {
        v = g[u][i];
        du[v]--;
        if (du[v] == 0) {
          if (a[v] == 0)
            q1.push(v);
          else
            q2.push(v);
        }
      }
    }
    ok = 0;
    while (!q2.empty()) {
      ok = 1;
      u = q2.front();
      q2.pop();
      for (int i = (0); i < ((int)g[u].size()); i++) {
        v = g[u][i];
        du[v]--;
        if (du[v] == 0) {
          if (a[v] == 0)
            q1.push(v);
          else
            q2.push(v);
        }
      }
    }
    if (ok) ans++;
    if (q1.empty() && q2.empty()) break;
  }
  cout << ans;
  return 0;
}
