#include <bits/stdc++.h>
long long mpow(long long a, long long n, long long mod) {
  long long ret = 1;
  long long b = a;
  while (n) {
    if (n & 1) ret = (ret * b) % mod;
    b = (b * b) % mod;
    n >>= 1;
  }
  return (long long)ret;
}
inline void read(int &x) {
  x = 0;
  register char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 3) + (x << 1) + (c - '0');
}
inline void write(int x) {
  register char buffor[35];
  register int i = 0;
  do {
    buffor[i++] = (x % 10) + '0';
    x /= 10;
  } while (x);
  i--;
  while (i >= 0) putchar(buffor[i--]);
  putchar('\n');
}
inline int in() {
  int x;
  read(x);
  return x;
}
void solve();
int dbg = 1;
using namespace std;
int main() {
  int t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
  return 0;
}
vector<int> vec[300005], tobe, v1, v2, vec2[300005], done;
int ina[300005], a[300005], notd[300005];
void solve() {
  int n, m, x, y, ans = 0, tot = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    vec[y].push_back(x);
    vec2[x].push_back(y);
    ina[x]++;
  }
  for (int i = 0; i < n; i++) {
    if (ina[i] == 0) v1.push_back(i);
  }
  while (tot < n) {
    int first = 0;
    while (v1.size()) {
      tobe = v1;
      v1.clear();
      for (auto p : tobe) {
        for (auto u : vec[p]) {
          ina[u]--;
          if (ina[u] == 0) {
            if (a[u]) {
              v1.push_back(u);
              continue;
            }
            int f11 = 0;
            for (auto qq : vec2[u]) {
              if (a[qq] && !notd[qq]) {
                v2.push_back(u);
                f11 = 1;
                break;
              }
            }
            if (!f11) v1.push_back(u);
          }
        }
        tot++;
        done.push_back(p);
        if (a[p]) first = 1;
      }
    }
    ans += first;
    v1 = v2;
    v2.clear();
    for (auto p : done) notd[p] = 1;
    done.clear();
  }
  cout << ans << endl;
}
