#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100009;
int tot, n, m, head[MAXN], f[MAXN], in[MAXN], a[MAXN], ans;
bool vis[MAXN];
struct Edge {
  int to, next;
} edge[MAXN * 2];
void init() {
  tot = 0;
  for (int i = 0; i < MAXN; i++) {
    head[i] = -1;
    vis[i] = in[i] = a[i] = 0;
  }
}
void addedge(int x, int y) {
  edge[tot].to = y;
  edge[tot].next = head[x];
  head[x] = tot++;
}
void dfs(int x) {
  vis[x] = 1;
  ans = max(ans, a[x]);
  for (int i = head[x]; i != -1; i = edge[i].next) {
    int y = edge[i].to, tmp = 0;
    if (f[y] && (!f[x]))
      tmp = max(a[y], a[x] + 1);
    else
      tmp = max(a[y], a[x]);
    if (!vis[y] || tmp != a[y]) {
      a[y] = tmp;
      dfs(y);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  init();
  for (int i = 0; i < n; ++i) scanf("%d", &f[i]);
  for (int i = 0, x, y; i < m; ++i) {
    scanf("%d%d", &x, &y);
    addedge(y, x);
    in[x]++;
  }
  ans = 0;
  for (int i = 0; i < n; ++i) {
    if (in[i] == 0) {
      a[i] = f[i];
      dfs(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
