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