#include <bits/stdc++.h>
using namespace std;
long long int in[100010], vis[100010], a[100010], dp[100010];
vector<long long int> v[100010];
long long int ans;
void dfs(long long int i) {
  if (v[i].size() == 0 && a[i] && !vis[i]) {
    ++dp[i];
  }
  vis[i] = 1;
  for (long long int j = 0; j < v[i].size(); j++) {
    if (!vis[v[i][j]]) {
      dfs(v[i][j]);
    }
    if (a[i] == 1 && a[v[i][j]] == 0)
      dp[i] = max(dp[i], dp[v[i][j]] + 1);
    else
      dp[i] = max(dp[i], dp[v[i][j]]);
  }
  ans = max(ans, dp[i]);
}
int main() {
  long long int n, m, x, y;
  cin >> n >> m;
  for (long long int i = 0; i < n; i++) cin >> a[i];
  for (long long int i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    ++in[y];
  }
  for (long long int i = 0; i < n; i++)
    if (in[i] == 0) dfs(i);
  cout << ans << endl;
  return 0;
}
