#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
vector<int> graph[N];
int indeg[N], dp[N], ind[N];
bool cop[N];
int n, m;
signed main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = (1); i <= (n); i++) {
    cin >> cop[i];
  }
  for (int i = (1); i <= (m); i++) {
    int u, v;
    cin >> u >> v;
    u++;
    v++;
    graph[v].push_back(u);
    indeg[u]++;
    ind[u]++;
  }
  queue<int> q;
  for (int i = (1); i <= (n); i++) {
    if (!indeg[i]) {
      q.push(i);
    }
  }
  vector<int> ts;
  while (q.size()) {
    int u = q.front();
    q.pop();
    ts.push_back(u);
    for (auto v : graph[u]) {
      indeg[v]--;
      if (!indeg[v]) {
        q.push(v);
      }
    }
  }
  int ans = 0;
  for (int i = (0); i <= (n - 1); i++) {
    int u = ts[i];
    if (!ind[u]) {
      dp[u] = cop[u];
    }
    for (auto v : graph[u]) {
      dp[v] = max(dp[v], dp[u] + (!cop[u] && cop[v]));
    }
    ans = max(ans, dp[u]);
  }
  cout << ans << endl;
  return 0;
}
