#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return f ? x : -x;
}
const long long N = 1e5 + 5;
long long tot, ver[N], nxt[N], head[N], in[N];
void add(long long u, long long v) {
  ver[++tot] = v;
  nxt[tot] = head[u];
  head[u] = tot;
}
long long n, m, e[N], ans;
queue<long long> qm, qa;
void topo() {
  for (long long i = 1; i <= n; i++) {
    if (!in[i]) {
      if (e[i])
        qa.push(i);
      else
        qm.push(i);
    }
  }
  while (!qm.empty() || !qa.empty()) {
    while (!qm.empty()) {
      long long x = qm.front();
      qm.pop();
      for (long long i = head[x]; i; i = nxt[i]) {
        long long y = ver[i];
        in[y]--;
        if (!in[y]) {
          if (e[y])
            qa.push(y);
          else
            qm.push(y);
        }
      }
    }
    if (!qa.empty()) ans++;
    while (!qa.empty()) {
      long long x = qa.front();
      qa.pop();
      for (long long i = head[x]; i; i = nxt[i]) {
        long long y = ver[i];
        in[y]--;
        if (!in[y]) {
          if (e[y])
            qa.push(y);
          else
            qm.push(y);
        }
      }
    }
  }
}
signed main() {
  n = read(), m = read();
  for (long long i = 1; i <= n; i++) e[i] = read();
  for (long long i = 1; i <= m; i++) {
    long long u = read() + 1, v = read() + 1;
    add(v, u);
    in[u]++;
  }
  topo();
  printf("%lld", ans);
  return 0;
}
