#include <bits/stdc++.h>
#pragma GCC optimize "O3"
using namespace std;
constexpr int N = 100000;
int n, m, e[N], deg[N];
vector<int> adj[N];
queue<int> q[2];
void process(queue<int> &qq) {
  while (!qq.empty()) {
    int u = qq.front();
    qq.pop();
    for (int v : adj[u]) {
      --deg[v];
      if (deg[v] == 0) q[e[v]].push(v);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", e + i);
  for (int i = 0; i < m; ++i) {
    int v, u;
    scanf("%d %d", &v, &u);
    adj[u].push_back(v);
    ++deg[v];
  }
  for (int i = 0; i < n; ++i)
    if (deg[i] == 0) q[e[i]].push(i);
  int ans = 0;
  while (!q[0].empty() || !q[1].empty()) {
    process(q[0]);
    if (!q[1].empty()) {
      process(q[1]);
      ++ans;
    }
  }
  printf("%d\n", ans);
}
