#include <bits/stdc++.h>
using namespace std;
int a[100005];
vector<int> vec[100005];
int vis[100005];
int vi[100005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  memset(vis, 0, sizeof(vis));
  memset(vi, 0, sizeof(vi));
  int u, v;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    vec[v].push_back(u);
    vis[u]++;
  }
  queue<int> que;
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      if (a[i + 1] == 0)
        que.push(i);
      else
        q.push(i);
    }
  }
  int cnt = 0;
  while (que.size() != 0 || q.size() != 0) {
    while (que.size() != 0) {
      int t = que.front();
      que.pop();
      if (vi[t] == 1) continue;
      vi[t] = 1;
      for (int i = 0; i < vec[t].size(); i++) {
        vis[vec[t][i]]--;
        if (vis[vec[t][i]] == 0) {
          if (a[vec[t][i] + 1] == 0)
            que.push(vec[t][i]);
          else
            q.push(vec[t][i]);
        }
      }
    }
    if (q.size() != 0) {
      cnt++;
      while (q.size() != 0) {
        int t = q.front();
        q.pop();
        if (vi[t] == 1) continue;
        vi[t] = 1;
        for (int i = 0; i < vec[t].size(); i++) {
          vis[vec[t][i]]--;
          if (vis[vec[t][i]] == 0) {
            if (a[vec[t][i] + 1] == 0)
              que.push(vec[t][i]);
            else
              q.push(vec[t][i]);
          }
        }
      }
    }
  }
  printf("%d\n", cnt);
  return 0;
}
