#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, mark[N], deg[N], tmp[N];
vector<int> g[N];
int Bfs(int k) {
  stack<int> st[2];
  for (int i = 0; i < n; ++i)
    if (!deg[i]) st[mark[i]].push(i);
  int cur = k, cnt = 0;
  while (st[0].size() + st[1].size() > 0) {
    if (st[cur].empty()) cur = 1 - cur, cnt += (cur == 0);
    int u = st[cur].top();
    st[cur].pop();
    for (int v : g[u]) {
      --deg[v];
      if (!deg[v]) st[mark[v]].push(v);
    }
  }
  cnt += (cur == 1);
  return cnt;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", &mark[i]);
  for (int i = 0, u, v; i < m; ++i) {
    scanf("%d%d", &v, &u);
    ++deg[v], ++tmp[v], g[u].push_back(v);
  }
  int res = Bfs(0);
  for (int i = 0; i < n; ++i) deg[i] = tmp[i];
  res = min(res, Bfs(1));
  cout << res;
  return 0;
}
