#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<long long> e, u, ord, t;
vector<vector<long long> > g, h;
void dfs(int v) {
  u[v] = 1;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!u[to]) dfs(to);
  }
  ord.push_back(v);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  g.resize(n);
  h.resize(n);
  e.resize(n);
  for (int i = 0; i < n; ++i) cin >> e[i];
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    h[b].push_back(a);
  }
  u.assign(n, 0);
  for (int i = 0; i < n; ++i)
    if (!u[i]) dfs(i);
  t.resize(n);
  for (int i = 0; i < n; ++i) {
    int v = ord[i];
    t[v] = e[v];
    for (int j = 0; j < g[v].size(); ++j) {
      int to = g[v][j];
      t[v] = max(t[v], t[to] + (e[v] && !e[to]));
    }
  }
  cout << *max_element(t.begin(), t.end()) << endl;
  return 0;
}
