#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, m;
int arr[N];
vector<vector<int> > v(N);
int in[N];
queue<int> q0, q1;
void bfs1();
int cntr = 0;
void bfs0() {
  while (q0.size()) {
    int node = q0.front();
    q0.pop();
    for (int i = 0; i < v[node].size(); i++) {
      int cur = v[node][i];
      in[cur]--;
      if (!in[cur]) {
        if (arr[cur] == 1)
          q1.push(cur);
        else
          q0.push(cur);
      }
    }
  }
  if (q1.size()) bfs1();
}
void bfs1() {
  cntr++;
  while (q1.size()) {
    int node = q1.front();
    q1.pop();
    for (int i = 0; i < v[node].size(); i++) {
      int cur = v[node][i];
      in[cur]--;
      if (!in[cur]) {
        if (arr[cur] == 1)
          q1.push(cur);
        else
          q0.push(cur);
      }
    }
  }
  if (q0.size()) bfs0();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  int x, y;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    in[x]++;
    v[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (!in[i] && !arr[i]) q0.push(i);
    if (!in[i] && arr[i]) q1.push(i);
  }
  if (q0.size()) bfs0();
  if (q1.size()) bfs1();
  printf("%d\n", cntr);
  return 0;
}
