#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
bool Finish_read;
template <class T>
inline void read(T &x) {
  Finish_read = 0;
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    if (ch == EOF) return;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  x *= f;
  Finish_read = 1;
}
template <class T>
inline void print(T x) {
  if (x / 10 != 0) print(x / 10);
  putchar(x % 10 + '0');
}
template <class T>
inline void writeln(T x) {
  if (x < 0) putchar('-');
  x = abs(x);
  print(x);
  putchar('\n');
}
template <class T>
inline void write(T x) {
  if (x < 0) putchar('-');
  x = abs(x);
  print(x);
}
int d[100010], t[100010], f[100010], n, m;
vector<int> G[100010], O;
queue<int> q;
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) read(t[i]);
  for (int i = 1, x, y; i <= m; i++) {
    read(x);
    read(y);
    x++;
    y++;
    G[y].push_back(x);
    d[x]++;
  }
  for (int i = 1; i <= n; i++)
    if (!d[i]) {
      q.push(i);
      f[i] = t[i];
    }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    O.push_back(u);
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      d[v]--;
      if (!d[v]) q.push(v);
    }
  }
  for (int i = 0; i < O.size(); i++) {
    int u = O[i];
    for (int j = 0; j < G[u].size(); j++) {
      int v = G[u][j];
      if (t[v] && !t[u])
        f[v] = max(f[v], f[u] + 1);
      else
        f[v] = max(f[v], f[u]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (t[i]) ans = max(ans, f[i]);
  printf("%d\n", ans);
}
