#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 5;
int type[MAXN];
vector<int> g[MAXN];
vector<int> p[MAXN];
int depA[MAXN];
int bready = 0;
int btotal = 0;
int ans = 0;
int it = 0;
int deg[MAXN];
set<int> sA, sB;
void dfsA(int u) {
  for (int v : p[u]) {
    deg[v]--;
    if (deg[v] == 0 && type[v] == 0)
      dfsA(v);
    else if (deg[v] == 0 && type[v] == 1)
      sB.insert(v);
  }
}
void dfsB(int u) {
  bready++;
  for (int v : p[u]) {
    deg[v]--;
    if (deg[v] == 0 && type[v] == 1)
      dfsB(v);
    else if (deg[v] == 0 && type[v] == 0)
      sA.insert(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> type[i];
    if (type[i] == 1) btotal++;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    p[v].push_back(u);
    deg[u]++;
    if (type[v] == 0) depA[u]++;
  }
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0 && type[i] == 0) {
      sA.insert(i);
    }
    if (deg[i] == 0 && type[i] == 1) {
      sB.insert(i);
    }
  }
  for (int u : sA) {
    dfsA(u);
  }
  sA.clear();
  while (bready < btotal) {
    ans++;
    if (sB.size() != 0)
      for (int u : sB) dfsB(u);
    sB.clear();
    if (sA.size() != 0)
      for (int v : sA) dfsA(v);
    sA.clear();
    it = 1 - it;
  }
  cout << ans;
}
