#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
template <class T>
void dbs(string str, T t) {
  cout << str << " : " << t << endl;
}
template <class T, class... second>
void dbs(string str, T t, second... s) {
  int idx = str.find(',');
  cout << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class second, class T>
ostream& operator<<(ostream& os, const pair<second, T>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class second, class T>
ostream& operator<<(ostream& os, const map<second, T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
void prc(T a, T b) {
  cout << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cout << ", ";
    cout << *i;
  }
  cout << "]";
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = (0); i <= (n - 1); ++i) cin >> a[i];
  vector<vector<int> > g(n);
  vector<int> d(n);
  while (m--) {
    int x, y;
    cin >> x >> y;
    g[y].push_back(x);
    d[x]++;
  }
  vector<vector<int> > M(2);
  for (int i = (0); i <= (n - 1); ++i) {
    if (d[i] == 0) {
      M[a[i]].push_back(i);
    }
  }
  int ans = 0;
  while (int(M[0].size()) + int(M[1].size())) {
    if (!M[0].empty()) {
      int x = M[0].back();
      M[0].pop_back();
      for (int y : g[x]) {
        --d[y];
        if (d[y] == 0) M[a[y]].push_back(y);
      }
    } else {
      ++ans;
      while (!M[1].empty()) {
        int x = M[1].back();
        M[1].pop_back();
        for (int y : g[x]) {
          --d[y];
          if (d[y] == 0) M[a[y]].push_back(y);
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
