#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int k = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    k = k * 10 + ch - '0';
    ch = getchar();
  }
  return k * f;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(int x) {
  write(x);
  puts("");
}
int nedge = 0, p[200010], nex[200010], head[200010];
int a[100010], n, m, f[100010], rd[100010];
queue<int> q1, q2;
inline void addedge(int a, int b) {
  p[++nedge] = b;
  nex[nedge] = head[a];
  head[a] = nedge;
}
inline void work() {
  while (!q2.empty()) {
    int now = q2.front();
    q2.pop();
    for (int k = head[now]; k; k = nex[k]) {
      rd[p[k]]--;
      if (rd[p[k]]) continue;
      if (a[p[k]])
        q1.push(p[k]);
      else
        q2.push(p[k]);
    }
  }
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= m; i++) {
    int x = read() + 1, y = read() + 1;
    addedge(y, x);
    rd[x]++;
  }
  for (int i = 1; i <= n; i++)
    if (!rd[i]) {
      if (a[i])
        q1.push(i);
      else
        q2.push(i);
    }
  work();
  int ans = 0;
  while (!q1.empty()) {
    ans++;
    while (!q1.empty()) {
      int now = q1.front();
      q1.pop();
      for (int k = head[now]; k; k = nex[k]) {
        rd[p[k]]--;
        if (rd[p[k]]) continue;
        if (a[p[k]])
          q1.push(p[k]);
        else
          q2.push(p[k]);
      }
    }
    work();
  }
  writeln(ans);
  return 0;
}
