#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
int n, head[MAXN], m, cnt, e[MAXN], dig[MAXN];
struct Edge {
  int v, nex;
} edge[MAXN * 2];
void add(int u, int v) {
  edge[cnt] = {v, head[u]};
  head[u] = cnt++;
}
queue<int> q[2];
void clear(int o) {
  while (q[o].size()) {
    int u = q[o].front();
    q[o].pop();
    for (int i = head[u]; ~i; i = edge[i].nex) {
      int v = edge[i].v;
      dig[v]--;
      if (dig[v] == 0) q[e[v]].push(v);
    }
  }
}
int cal() {
  int ret = 0;
  for (int i = 1; i <= n; i++)
    if (!dig[i]) q[e[i]].push(i);
  while (q[0].size() + q[1].size()) {
    clear(0);
    if (q[0].size() + q[1].size() == 0) break;
    clear(1);
    ret++;
  }
  return ret;
}
int main() {
  memset(head, -1, sizeof head);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> e[i];
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    u++, v++;
    add(v, u);
    dig[u]++;
  }
  cout << cal() << endl;
  return 0;
}
