#include <bits/stdc++.h>
using namespace std;
int n, m;
bool imp[100005];
vector<int> g[100005];
int sol[100005], id[100005];
void toposort() {
  queue<int> q;
  for (int it = 0; it < n; it++) {
    if (!id[it]) {
      q.push(it);
      sol[it] = imp[it];
    }
  }
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i = 0; i < g[t].size(); i++) {
      int v = g[t][i];
      id[v]--;
      sol[v] = max(sol[v], sol[t] + ((imp[t] == 0) && (imp[v] == 1)));
      if (id[v] == 0) {
        q.push(v);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> imp[i];
  }
  for (int i = 0; i < m; i++) {
    pair<int, int> a;
    cin >> a.first >> a.second;
    g[a.first].push_back(a.second);
    id[a.second]++;
  }
  toposort();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, sol[i]);
  }
  cout << ans;
  return 0;
}
