#include <bits/stdc++.h>
using namespace std;
int N, M;
int E[100000];
vector<int> A[100000];
vector<int> B[100000];
vector<int> topo;
bool mark[100000];
int dp[100000];
void visit(int n) {
  if (mark[n]) return;
  mark[n] = true;
  for (int i = 0; i < A[n].size(); i++) visit(A[n][i]);
  topo.push_back(n);
}
void topo_sort() {
  for (int i = 0; i < N; i++)
    if (!mark[i]) visit(i);
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> E[i];
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    A[a].push_back(b);
    B[b].push_back(a);
  }
  fill(mark, mark + N, false);
  topo_sort();
  fill(dp, dp + N, 0);
  for (int i = 0; i < N; i++) {
    int n = topo[i];
    if (A[n].size() == 0)
      dp[n] = E[n];
    else {
      if (E[n] == 0) {
        for (int i = 0; i < A[n].size(); i++) {
          int m = A[n][i];
          dp[n] = max(dp[n], dp[m]);
        }
      } else {
        for (int i = 0; i < A[n].size(); i++) {
          int m = A[n][i];
          if (E[m] == 1)
            dp[n] = max(dp[n], dp[m]);
          else
            dp[n] = max(dp[n], dp[m] + 1);
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < N; i++) res = max(res, dp[i]);
  cout << res << endl;
  return 0;
}
