#include <bits/stdc++.h>
using namespace std;
istream& in = cin;
int n, m;
vector<int> vi[100010];
vector<int> tree[100010];
int deg[100010];
int E[100010];
void input() {
  in >> n >> m;
  for (int i = 0; i < n; ++i) in >> E[i];
  for (int i = 1; i <= m; ++i) {
    int a, b;
    in >> a >> b;
    tree[a].push_back(b);
    tree[b].push_back(a);
    vi[b].push_back(a);
    deg[a]++;
  }
}
int color[100010];
void dfs(int p, int c) {
  if (color[p] == c) {
    return;
  }
  color[p] = c;
  for (int son : tree[p]) {
    dfs(son, c);
  }
}
vector<int> root[100010];
int cale(int idx) {
  int ans = 0;
  queue<int> Q[2];
  for (int v : root[idx]) {
    Q[E[v]].push(v);
  }
  while (!Q[0].empty()) {
    int u = Q[0].front();
    Q[0].pop();
    for (int e : vi[u]) {
      deg[e]--;
      if (deg[e] == 0) {
        Q[E[e]].push(e);
      }
    }
  }
  while (!Q[0].empty() || !Q[1].empty()) {
    if (!Q[1].empty()) {
      ans++;
    }
    while (!Q[1].empty()) {
      int u = Q[1].front();
      Q[1].pop();
      for (int e : vi[u]) {
        deg[e]--;
        if (deg[e] == 0) {
          Q[E[e]].push(e);
        }
      }
    }
    while (!Q[0].empty()) {
      int u = Q[0].front();
      Q[0].pop();
      for (int e : vi[u]) {
        deg[e]--;
        if (deg[e] == 0) {
          Q[E[e]].push(e);
        }
      }
    }
  }
  return ans;
}
int main() {
  int TEST_CASE = 1;
  while (TEST_CASE-- > 0) {
    input();
    memset(color, -1, sizeof(color));
    for (int i = 0; i < n; ++i) {
      if (color[i] == -1) {
        dfs(i, i);
      }
    }
    for (int i = 0; i < n; ++i) {
      if (deg[i] == 0) {
        root[color[i]].push_back(i);
      }
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int ret = cale(i);
      ans = max(ans, ret);
    }
    cout << ans << endl;
  }
  return 0;
}
