#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, a[N], deg[N];
vector<int> G[N];
queue<int> q;
int dis[N];
int toposort() {
  memset(dis, 0, sizeof(dis));
  for (int i = 1; i <= n; i++)
    if (deg[i] == 0) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < G[x].size(); i++) {
      int y = G[x][i];
      if (a[x] == 1 && a[y] == 0)
        dis[y] = max(dis[y], dis[x] + 1);
      else
        dis[y] = max(dis[y], dis[x]);
      if (--deg[y] == 0) q.push(y);
    }
  }
  return dis[n + 1];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x++;
    y++;
    G[x].push_back(y);
    deg[y]++;
  }
  for (int i = 1; i <= n; i++) {
    G[i].push_back(n + 1);
    deg[n + 1]++;
  }
  cout << toposort();
  return 0;
}
