#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int es[MAX_N], pns[MAX_N];
vector<int> nbrs[MAX_N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", es + i);
  for (int i = 0; i < m; i++) {
    int t0, t1;
    scanf("%d%d", &t0, &t1);
    nbrs[t1].push_back(t0);
    pns[t0]++;
  }
  queue<int> mq, cq;
  for (int i = 0; i < n; i++)
    if (pns[i] == 0) {
      if (es[i] == 0)
        mq.push(i);
      else
        cq.push(i);
    }
  int cnt = 0;
  while (!mq.empty() || !cq.empty()) {
    while (!mq.empty()) {
      int u = mq.front();
      mq.pop();
      vector<int> &nbru = nbrs[u];
      for (vector<int>::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
        int v = *vit;
        if (--pns[v] == 0) {
          if (es[v] == 0)
            mq.push(v);
          else
            cq.push(v);
        }
      }
    }
    if (!cq.empty()) {
      while (!cq.empty()) {
        int u = cq.front();
        cq.pop();
        vector<int> &nbru = nbrs[u];
        for (vector<int>::iterator vit = nbru.begin(); vit != nbru.end();
             vit++) {
          int v = *vit;
          if (--pns[v] == 0) {
            if (es[v] == 0)
              mq.push(v);
            else
              cq.push(v);
          }
        }
      }
      cnt++;
    }
  }
  printf("%d\n", cnt);
  return 0;
}
