#include <bits/stdc++.h>
long long int const MOD = 1000000007;
long long int const N = 1000005;
long long int const LN = 20;
long long int const inf = 8e18;
using namespace std;
long long int n, m, a[N];
long long int tp[N];
vector<long long int> v[N], topo;
long long int vis[N];
long long int dp[N];
void dfs(long long int u) {
  vis[u] = 1;
  for (auto w : v[u])
    if (!vis[w]) dfs(w);
  topo.push_back(u);
}
void topological_sort() {
  for (long long int i = 1; i <= n; i++) vis[i] = 0;
  topo.clear();
  for (long long int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  reverse(topo.begin(), topo.end());
}
bool iscycle() {
  for (long long int i = 1; i <= n; i++) vis[i] = 0;
  for (long long int i = 0; i < n; i++) {
    vis[topo[i]] = 1;
    for (auto w : v[topo[i]]) {
      if (vis[w]) return true;
    }
  }
  return false;
}
int main() {
  long long int m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) cin >> tp[i];
  for (long long int i = 1; i <= m; i++) {
    long long int a, b;
    cin >> a >> b;
    a++;
    b++;
    v[b].push_back(a);
  }
  topological_sort();
  long long int sz = topo.size();
  for (long long int i = sz - 1; i >= 0; i--) {
    long long int ver = topo[i];
    long long int temp = 0;
    if (tp[ver]) {
      for (auto w : v[ver]) {
        if (tp[w])
          temp = max(temp, dp[w] - 1);
        else
          temp = max(temp, dp[w]);
      }
    } else {
      temp--;
      for (auto w : v[ver]) temp = max(temp, dp[w] - 1);
    }
    dp[ver] = temp + 1;
  }
  long long int ans = 0;
  for (long long int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}
