#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
int n, m;
int kind[MAXN];
vector<int> dep[MAXN], rdep[MAXN];
int rem;
int outdeg[MAXN];
queue<int> q[2];
void process(int z) {
  while (!q[z].empty()) {
    int at = q[z].front();
    q[z].pop();
    --rem;
    for (int i = (0); i < (((int)(rdep[at]).size())); ++i) {
      int to = rdep[at][i];
      if (--outdeg[to] == 0) q[kind[to]].push(to);
    }
  }
}
void run() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); ++i) scanf("%d", &kind[i]);
  for (int i = (0); i < (m); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    dep[a].push_back(b);
    rdep[b].push_back(a);
  }
  for (int i = (0); i < (n); ++i) outdeg[i] = ((int)(dep[i]).size());
  for (int j = (0); j < (2); ++j) q[j] = queue<int>();
  for (int i = (0); i < (n); ++i)
    if (outdeg[i] == 0) q[kind[i]].push(i);
  int ret = 0;
  rem = n;
  while (rem > 0) {
    process(0);
    if (rem == 0) break;
    ++ret;
    process(1);
  }
  printf("%d\n", ret);
}
int main() {
  run();
  return 0;
}
