#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int color[100010], in[100010];
queue<int> q[2];
vector<int> E[100010];
int main() {
  int n, m, x, y;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &color[i]);
  for (int i = 1; i <= m; ++i) {
    scanf("%d %d", &x, &y);
    ++x, ++y;
    E[y].push_back(x);
    in[x]++;
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (!in[i]) q[color[i]].push(i);
  while (!q[0].empty() || !q[1].empty()) {
    while (!q[0].empty()) {
      x = q[0].front();
      q[0].pop();
      for (auto y : E[x])
        if (--in[y] == 0) q[color[y]].push(y);
    }
    if (!q[1].empty()) {
      ++ans;
      while (!q[1].empty()) {
        x = q[1].front();
        q[1].pop();
        for (auto y : E[x])
          if (--in[y] == 0) q[color[y]].push(y);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
