#include <bits/stdc++.h>
using namespace std;
int N, M, E[100005];
vector<int> adj[100005], rev[100005];
int dp[100005];
int solve(int x) {
  if (dp[x] != -1) return dp[x];
  for (int i = 0; i < adj[x].size(); i++) {
    dp[x] = max(dp[x],
                solve(adj[x][i]) + ((E[x] == 1 && E[adj[x][i]] == 0) ? 1 : 0));
  }
  if (adj[x].size() == 0) dp[x] = E[x];
  return dp[x];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> E[i];
  for (int i = 0; i < M; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    adj[t1].push_back(t2);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) dp[i] = -1;
  for (int i = 0; i < N; i++) {
    ans = max(ans, solve(i));
  }
  cout << ans << "\n";
}
