#include <bits/stdc++.h>
using namespace std;
int n, m;
set<int> st;
set<int> nxt_run;
vector<int> nxt[100100];
int flag[100100];
int topo[100100];
int ref__[100100];
int ftime__[100100];
int main() {
  cin >> n >> m;
  for (int i = 0; i <= n - 1; i++) scanf("%d", &flag[i]);
  int a, b;
  for (int i = 0; i <= m - 1; i++) {
    scanf("%d %d", &a, &b);
    nxt[b].push_back(a);
    ref__[a]++;
  }
  for (int i = 0; i <= n - 1; i++) {
    if (ref__[i])
      st.insert(i);
    else
      ftime__[i] = 1;
  }
  for (int i = 0; i <= n - 1; i++) {
    if (ftime__[i]) {
      vector<int> tmp = nxt[i];
      int len = tmp.size();
      for (int j = 0; j <= len - 1; j++) {
        ref__[tmp[j]]--;
        if (ref__[tmp[j]] == 0) {
          st.erase(tmp[j]);
          nxt_run.insert(tmp[j]);
        }
      }
      for (int j = 0; j <= len - 1; j++)
        if (flag[i])
          topo[tmp[j]] = max(topo[tmp[j]], topo[i] + (flag[tmp[j]] == 0));
        else
          topo[tmp[j]] = max(topo[tmp[j]], topo[i]);
    }
  }
  while (st.size()) {
    set<int> new_set;
    set<int>::iterator it;
    for (it = nxt_run.begin(); it != nxt_run.end(); ++it) {
      int t = *it;
      vector<int> tmp = nxt[t];
      int len = tmp.size();
      for (int i = 0; i <= len - 1; i++) {
        ref__[tmp[i]]--;
        if (ref__[tmp[i]] == 0) {
          st.erase(tmp[i]);
          new_set.insert(tmp[i]);
        }
      }
      for (int i = 0; i <= len - 1; i++)
        if (flag[t])
          topo[tmp[i]] = max(topo[tmp[i]], topo[t] + (flag[tmp[i]] == 0));
        else
          topo[tmp[i]] = max(topo[tmp[i]], topo[t]);
    }
    nxt_run = new_set;
  }
  int ans = 0;
  for (int i = 0; i <= n - 1; i++) {
    ans = max(ans, topo[i] + flag[i]);
  }
  cout << ans;
}
