#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
bool vis[100001], e[100001], ok[100001];
int d[100001];
vector<int> gv[100001];
void dfs(int x) {
  vis[x] = 1;
  if (e[x]) d[x] = 1;
  for (int i = 0; i < gv[x].size(); i++) {
    int son = gv[x][i];
    if (!vis[son]) dfs(son);
    if (d[x] <= d[son]) {
      if (e[x]) {
        if (e[son])
          d[x] = d[son];
        else
          d[x] = d[son] + 1;
      } else
        d[x] = d[son];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> e[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    gv[x].push_back(y);
    ok[y] = 1;
  }
  for (int i = 0; i < n; i++)
    if (!ok[i]) gv[n].push_back(i);
  dfs(n);
  cout << d[n];
}
