#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100005], topoSort;
int processor[100005], dp[100005];
bool visited[100005];
void dfs(int vertex) {
  int i;
  for (i = 0; i < graph[vertex].size(); i++) {
    int temp = graph[vertex][i];
    if (visited[temp] == false) {
      visited[temp] = true;
      dfs(temp);
      topoSort.push_back(temp);
    }
  }
  return;
}
int main() {
  int i, j, t, n, m, k, l, r, temp, mini, maxi, flag, cnt, ans = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%d", &processor[i]);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &l, &r);
    graph[l + 1].push_back(r + 1);
  }
  for (i = 1; i <= n; i++) {
    if (visited[i] == false) {
      visited[i] = true;
      dfs(i);
      topoSort.push_back(i);
    }
  }
  for (k = 0; k < n; k++) {
    i = topoSort[k];
    if (processor[i] == 0) {
      maxi = 0;
      for (j = 0; j < graph[i].size(); j++) maxi = max(maxi, dp[graph[i][j]]);
    } else {
      maxi = 1;
      for (j = 0; j < graph[i].size(); j++) {
        temp = graph[i][j];
        maxi = max(maxi, dp[temp] + (processor[temp] == 0));
      }
    }
    dp[i] = maxi;
    ans = max(ans, dp[i]);
  }
  printf("%d\n", ans);
  return 0;
}
