#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 10;
long long int n, ma, k[N], d[N], e[N], x, y;
queue<long long int> q;
vector<long long int> v[N];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> ma;
  fill(e, e + n + 1, 1);
  for (int i = 0; i < n; i++) cin >> k[i];
  for (int i = 1; i <= ma; i++) {
    cin >> x >> y;
    d[x]++;
    v[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (d[i] == 0) q.push(i);
  }
  while (!q.empty()) {
    x = q.front(), q.pop();
    for (int i = 0; i < v[x].size(); i++) {
      y = v[x][i];
      d[y]--;
      if (k[x] == 1 && k[y] == 0)
        e[y] = max(e[y], e[x] + 1);
      else
        e[y] = max(e[y], e[x]);
      if (d[y] == 0) q.push(y);
    }
  }
  x = 0;
  for (int i = 0; i < n; i++) {
    if (k[i] == 1) x = max(x, e[i]);
  }
  cout << x;
  return 0;
}
