#include <bits/stdc++.h>
using namespace std;
int gcd1(int a, int b) {
  if (a == 0) return b;
  return gcd1(b % a, a);
}
long long modx(long long base, long long ex) {
  long long ans = 1LL, val = base;
  while (ex > 0LL) {
    if (ex & 1LL) ans = (ans * val) % 1000000009LL;
    val = (val * val) % 1000000009LL;
    ex = ex >> 1LL;
  }
  return ans;
}
int n, m;
int e[100005];
vector<int> adj[100005];
stack<int> s;
bool visit[100005];
int res[100005];
void dfs(int start) {
  visit[start] = true;
  for (int i = 0; i < adj[start].size(); i++) {
    int pt = adj[start][i];
    if (!visit[pt]) dfs(pt);
  }
  s.push(start);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> e[i];
  int t1, t2;
  for (int i = 1; i <= m; i++) {
    cin >> t1 >> t2;
    adj[t2].push_back(t1);
  }
  for (int i = 0; i < n; i++)
    if (!visit[i]) dfs(i);
  int ans = 0;
  while (!s.empty()) {
    int t = s.top();
    if (e[t]) res[t] = max(res[t], 1);
    ans = max(ans, res[t]);
    s.pop();
    for (int i = 0; i < adj[t].size(); i++) {
      int pt = adj[t][i];
      if (!e[t]) {
        if (e[pt])
          res[pt] = max(res[pt], res[t] + 1);
        else
          res[pt] = max(res[pt], res[t]);
      } else
        res[pt] = max(res[pt], res[t]);
    }
  }
  cout << ans << endl;
  return 0;
}
