#include <bits/stdc++.h>
using namespace std;
queue<int> q[2];
int a[100005];
vector<int> g[100005];
int deg[100005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a++, b++;
    g[b].push_back(a);
    deg[a]++;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 0) q[a[i]].push(i);
  while (!q[0].empty() || !q[1].empty()) {
    while (!q[0].empty()) {
      int u = q[0].front();
      q[0].pop();
      for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        deg[v]--;
        if (deg[v] == 0) q[a[v]].push(v);
      }
    }
    bool check = 0;
    while (!q[1].empty()) {
      if (!check) ans++;
      check = 1;
      int u = q[1].front();
      q[1].pop();
      for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        deg[v]--;
        if (deg[v] == 0) q[a[v]].push(v);
      }
    }
  }
  printf("%d\n", ans);
}
