#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> in_edge[N];
vector<int> out_edge[N];
int c[N];
int dp[N];
int in_deg[N];
void add(int x, int y) {
  in_edge[y].push_back(x);
  in_deg[y]++;
  out_edge[x].push_back(y);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int x, y;
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    add(y, x);
  }
  queue<int> Q;
  for (int i = 0; i < n; i++) {
    if (in_deg[i] == 0) {
      Q.push(i);
      if (c[i] == 0)
        dp[i] = 0;
      else
        dp[i] = 1;
    }
  }
  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();
    for (int from : in_edge[now]) {
      if (c[now] == 1) {
        dp[now] = max(dp[now], dp[from] + ((c[from] == 0) ? 1 : 0));
      } else {
        dp[now] = max(dp[now], dp[from]);
      }
    }
    for (int to : out_edge[now]) {
      in_deg[to]--;
      if (in_deg[to] == 0) Q.push(to);
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, dp[i]);
  }
  printf("%d\n", res);
}
