#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long mp[100005];
vector<long long> adj[100005];
bool visited[100005];
long long T[100005];
vector<long long> topo;
void dfs(int node) {
  visited[node] = true;
  for (auto i : adj[node]) {
    if (!visited[i]) dfs(i);
  }
  topo.push_back(node);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a == 0)
      mp[i] = 0;
    else
      mp[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    adj[a].push_back(b);
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) dfs(i);
  }
  for (auto i : topo) {
    long long tm;
    if (mp[i] == 0)
      tm = 0;
    else
      tm = 1;
    for (auto j : adj[i]) {
      if (mp[i] == 0) {
        tm = max(tm, T[j]);
      } else {
        if (mp[j] == 0) {
          tm = max(tm, T[j] + 1);
        } else {
          tm = max(tm, T[j]);
        }
      }
    }
    T[i] = tm;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (mp[i] == 1) {
      ans = max(ans, T[i]);
    }
  }
  cout << ans;
}
