#include <bits/stdc++.h>
using namespace std;
int a[100010];
vector<int> v[100010];
int h[100010];
queue<int> q, p;
int main() {
  int n, m, i;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[y].push_back(x);
    h[x]++;
  }
  for (i = 0; i < n; i++)
    if (h[i] == 0) {
      if (a[i] == 0) q.push(i);
      if (a[i] == 1) p.push(i);
    }
  int k = 0;
  while (1) {
    while (!q.empty()) {
      n--;
      int x = q.front();
      q.pop();
      for (i = 0; i < v[x].size(); i++) {
        h[v[x][i]]--;
        if (h[v[x][i]] == 0)
          if (a[v[x][i]] == 0)
            q.push(v[x][i]);
          else
            p.push(v[x][i]);
      }
    }
    if (n == 0) break;
    k++;
    while (!p.empty()) {
      n--;
      int x = p.front();
      p.pop();
      for (i = 0; i < v[x].size(); i++) {
        h[v[x][i]]--;
        if (h[v[x][i]] == 0)
          if (a[v[x][i]] == 0)
            q.push(v[x][i]);
          else
            p.push(v[x][i]);
      }
    }
  }
  printf("%d\n", k);
  return 0;
}
