#include <bits/stdc++.h>
using namespace std;
char buf[1 << 15], *p1 = buf, *p2 = buf;
inline long long read() {
  char ch;
  bool flag = 0;
  long long res;
  while (!isdigit(
      ch = (p1 == p2 &&
                    (p2 = (p1 = buf) + fread(buf, 1, 1 << 15, stdin), p1 == p2)
                ? EOF
                : *p1++)))
    (ch == '-') && (flag = true);
  for (res = ch - '0'; isdigit(
           ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 15, stdin),
                             p1 == p2)
                     ? EOF
                     : *p1++));
       res = res * 10ll + ch - '0')
    ;
  (flag) && (res = -res);
  return res;
}
inline void write(long long x) {
  if (x < 0) x = ~x + 1ll, putchar('-');
  if (x > 9) write(x / 10ll);
  putchar(x % 10ll + '0');
}
int cnt = 0;
int head[101000];
struct node {
  int to, nxt;
} edge[301000];
void add(int x, int y) {
  edge[++cnt].to = y, edge[cnt].nxt = head[x];
  head[x] = cnt;
}
int bel[101000];
int deg[101000];
queue<int> q[2];
void solve() {
  int n = read(), m = read();
  for (register int i = (0); i <= (n - 1); i++) bel[i] = read();
  for (register int i = (1); i <= (m); i++) {
    int x = read(), y = read();
    add(y, x);
    deg[x]++;
  }
  for (register int i = (0); i <= (n - 1); i++) {
    if (deg[i] == 0) {
      q[bel[i]].push(i);
    }
  }
  int ans = 0;
  while (!q[0].empty() || !q[1].empty()) {
    while (!q[0].empty()) {
      int x = q[0].front();
      q[0].pop();
      for (int i = head[x]; i; i = edge[i].nxt) {
        int v = edge[i].to;
        deg[v]--;
        if (deg[v] == 0) {
          q[bel[v]].push(v);
        }
      }
    }
    if (!q[1].empty()) {
      ans++;
      while (!q[1].empty()) {
        int x = q[1].front();
        q[1].pop();
        for (int i = head[x]; i; i = edge[i].nxt) {
          int v = edge[i].to;
          deg[v]--;
          if (deg[v] == 0) {
            q[bel[v]].push(v);
          }
        }
      }
    }
  }
  cout << ans << endl;
}
int main() { solve(); }
