#include <bits/stdc++.h>
using namespace std;
const int mod = (1e9) + 7;
const int siz = (1e5) + 100;
int n, m;
vector<int> adj[siz];
int pr[siz];
int dep[siz];
struct cmp {
  bool operator()(int a, int b) const {
    if (pr[a] != pr[b]) return pr[a] > pr[b];
    return a < b;
  }
};
set<int, cmp> serve;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &pr[i]);
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    adj[b].push_back(a);
    dep[a]++;
  }
  for (int i = 0; i < n; i++) {
    if (dep[i] == 0) {
      serve.insert(i);
    }
  }
  bool still = false;
  int ans = 0;
  while (!serve.empty()) {
    vector<int> add, rem;
    int val = pr[still ? *serve.begin() : *serve.rbegin()];
    if (val == 0 && still) {
      still = false;
      ans++;
    } else if (val == 1) {
      still = true;
    }
    for (auto ite = still ? serve.begin() : serve.find(*serve.rbegin());
         ite != serve.end();) {
      int it = *ite;
      if (pr[it] != val) {
        break;
      }
      for (int i = 0; i < adj[it].size(); i++) {
        dep[adj[it][i]]--;
        if (dep[adj[it][i]] == 0) {
          add.push_back(adj[it][i]);
        }
      }
      rem.push_back(it);
      if (still)
        ite++;
      else {
        if (ite == serve.begin()) break;
        ite--;
      }
    }
    for (auto v : rem) {
      serve.erase(v);
    }
    for (auto v : add) {
      serve.insert(v);
    }
  }
  printf("%d\n", ans + still);
  return 0;
}
