#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) {
  long long z = y;
  while (x % y != 0) {
    z = x % y;
    x = y;
    y = z;
  }
  return z;
}
int e[100005], du[100005];
vector<int> mp[100005];
queue<int> q[2];
vector<int> ns[4];
int bd;
void fmain(int ID) {
  scanf("%d%d", &n, &m);
  for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", e + i);
  for (int(i) = 1; (i) <= (int)(m); (i)++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u++;
    v++;
    du[u]++;
    mp[v].push_back(u);
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (du[i] == 0) q[e[i]].push(i);
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (e[i]) bd++;
  int ans = 0;
  while (!q[0].empty() || !q[1].empty()) {
    if (bd == 0) break;
    while (!q[0].empty()) {
      int id = q[0].front();
      q[0].pop();
      for (int c : mp[id]) {
        du[c]--;
        if (du[c] == 0) {
          if (e[c] == 0)
            q[0].push(c);
          else
            q[1].push(c);
        }
      }
    }
    while (!q[1].empty()) {
      int id = q[1].front();
      q[1].pop();
      bd--;
      for (int c : mp[id]) {
        du[c]--;
        if (du[c] == 0) {
          if (e[c] == 0)
            q[0].push(c);
          else
            q[1].push(c);
        }
      }
    }
    ans++;
    if (bd == 0) break;
  }
  printf("%d\n", ans);
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
