#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int NMAX = 1e5 + 5;
int type[NMAX];
int in[NMAX];
vector<int> tr[NMAX];
int lv[NMAX];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = int(0); i <= int(n - 1); i++) cin >> type[i];
  for (int i = int(1); i <= int(m); i++) {
    int u, v;
    cin >> u >> v;
    tr[v].push_back(u);
    in[u]++;
  }
  priority_queue<pair<int, int> > pq;
  for (int i = int(0); i <= int(n - 1); i++) {
    lv[i] = type[i];
    if (in[i] == 0) pq.push({type[i], i});
  }
  int level = 0;
  while (!pq.empty()) {
    int l = pq.top().first;
    int v = pq.top().second;
    pq.pop();
    level = max(level, l);
    for (auto u : tr[v]) {
      in[u]--;
      if (type[v] == 1)
        lv[u] = max(lv[u], lv[v]);
      else if (type[u] == 1)
        lv[u] = max(lv[u], lv[v] + 1);
      else
        lv[u] = max(lv[u], lv[v]);
      if (in[u] == 0) {
        pq.push({lv[u], u});
      }
    }
  }
  cout << level << endl;
}
