#include <bits/stdc++.h>
using namespace std;
int dep[100005] = {}, co[100005] = {};
vector<int> v[100005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> co[i];
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    dep[x]++;
    v[y].push_back(x);
  }
  vector<int> coex, mex;
  for (int i = 0; i < n; i++) {
    if (dep[i] == 0) {
      if (co[i])
        coex.push_back(i);
      else
        mex.push_back(i);
    }
  }
  vector<int> cur_mex = mex, cur_coex = coex;
  vector<int> new_mex, new_coex;
  int ans = 0;
  while (1) {
    if (cur_mex.size() == 0 && cur_coex.size() == 0) break;
    while (cur_mex.size()) {
      for (int i = 0; i < cur_mex.size(); i++) {
        for (int j = 0; j < v[cur_mex[i]].size(); j++) {
          int to = v[cur_mex[i]][j];
          dep[to]--;
          if (dep[to] == 0) {
            if (co[to])
              cur_coex.push_back(to);
            else
              new_mex.push_back(to);
          }
        }
      }
      swap(cur_mex, new_mex);
      new_mex.clear();
    }
    if (cur_coex.size()) ans++;
    while (cur_coex.size()) {
      for (int i = 0; i < cur_coex.size(); i++) {
        for (int j = 0; j < v[cur_coex[i]].size(); j++) {
          int to = v[cur_coex[i]][j];
          dep[to]--;
          if (dep[to] == 0) {
            if (co[to])
              new_coex.push_back(to);
            else
              cur_mex.push_back(to);
          }
        }
      }
      swap(cur_coex, new_coex);
      new_coex.clear();
    }
  }
  cout << ans << "\n";
}
