#include <bits/stdc++.h>
using namespace std;
const int N = 200031;
int n, m, color[N];
vector<int> g[N];
vector<int> gr[N];
int indeg[N];
vector<int> leaves[3];
int cnt_dead;
int ans;
void kill(int v) {
  cnt_dead++;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    indeg[to]--;
    if (indeg[to] == 0) {
      leaves[color[to]].push_back(to);
    }
  }
}
void run_processor() {
  int v = leaves[0].back();
  leaves[0].pop_back();
  kill(v);
}
void run_coprocessor() {
  while (leaves[1].size()) {
    int v = leaves[1].back();
    leaves[1].pop_back();
    kill(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> color[i];
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    swap(a, b);
    g[a].push_back(b);
    gr[b].push_back(a);
    indeg[b]++;
  }
  for (int i = 0; i < n; i++) {
    if (indeg[i] == 0) leaves[color[i]].push_back(i);
  }
  while (true) {
    if (cnt_dead == n) break;
    if (leaves[0].size() > 0) {
      run_processor();
      continue;
    }
    run_coprocessor();
    ++ans;
  }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}
