#include <bits/stdc++.h>
using namespace std;
int arr[100100];
vector<int> adj[100100];
int cnt[100100];
queue<int> q[2];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[v].push_back(u);
    cnt[u]++;
  }
  for (int i = 0; i < n; ++i) {
    if (cnt[i] == 0) {
      q[arr[i]].push(i);
    }
  }
  int ans = 0;
  while (1) {
    int a = q[0].size(), b = q[1].size();
    if (a + b == 0) break;
    for (int i = 0; i < 2; ++i) {
      if (!q[i].empty() && i == 1) ans++;
      while (!q[i].empty()) {
        int u = q[i].front();
        q[i].pop();
        for (int j = 0; j < adj[u].size(); ++j) {
          int to = adj[u][j];
          cnt[to]--;
          if (!cnt[to]) {
            q[arr[to]].push(to);
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
