#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int maxe = 1e5 + 5;
struct Edge {
  int from, to, nxt;
} edges[maxe];
int n, m, a[maxn], lst[maxn], E_cnt;
int Q[2][maxn], hed[2], til[2], indeg[maxn];
inline int read() {
  int res = 0, f_f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f_f - 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) - '0' + ch, ch = getchar();
  return res * f_f;
}
inline void add_edge(int from, int to) {
  edges[++E_cnt] = (Edge){from, to, lst[from]};
  lst[from] = E_cnt;
}
inline int toposort() {
  int res = 0;
  hed[0] = hed[1] = 1, til[0] = til[1] = 0;
  for (int i = 1; i <= n; i++) {
    if (!indeg[i]) {
      Q[a[i]][++til[a[i]]] = i;
    }
  }
  while (hed[0] != til[0] + 1 || hed[1] != til[1] + 1) {
    while (hed[0] != til[0] + 1) {
      int u = Q[0][hed[0]];
      for (int i = lst[u]; ~i; i = edges[i].nxt) {
        Edge &e = edges[i];
        indeg[e.to]--;
        if (!indeg[e.to]) Q[a[e.to]][++til[a[e.to]]] = e.to;
      }
      hed[0]++;
    }
    if (hed[1] != til[1] + 1) res++;
    while (hed[1] != til[1] + 1) {
      int u = Q[1][hed[1]];
      for (int i = lst[u]; ~i; i = edges[i].nxt) {
        Edge &e = edges[i];
        indeg[e.to]--;
        if (!indeg[e.to]) Q[a[e.to]][++til[a[e.to]]] = e.to;
      }
      hed[1]++;
    }
  }
  return res;
}
int main() {
  memset(lst, -1, sizeof(lst));
  n = read(), m = read(), E_cnt = -1;
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= m; i++) {
    int from = read() + 1, to = read() + 1;
    add_edge(to, from), indeg[from]++;
  }
  printf("%d\n", toposort());
  return 0;
}
