#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > secik;
set<pair<int, int> >::iterator it;
vector<int> tab[100010], t;
int n, m, cur, x, y, in[100010], ans, c[100010];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int a = 1; a <= n; a++) cin >> c[a];
  for (int a = 1; a <= m; a++) {
    cin >> x >> y;
    ++x, ++y;
    tab[y].push_back(x);
    in[x]++;
  }
  for (int a = 1; a <= n; a++)
    if (in[a] == 0) secik.insert(make_pair(c[a], a));
  cur = 0;
  while (!secik.empty()) {
    if (cur == 0) {
      it = secik.begin();
      if (it->first == 0) {
        x = it->second;
        t.push_back(c[x]);
        for (int a = 0; a < tab[x].size(); a++) {
          in[tab[x][a]]--;
          if (in[tab[x][a]] == 0)
            secik.insert(make_pair(c[tab[x][a]], tab[x][a]));
        }
        secik.erase(*it);
      } else
        cur = 1;
    } else {
      it = secik.lower_bound(make_pair(1, 0));
      if (it != secik.end()) {
        x = it->second;
        t.push_back(c[x]);
        for (int a = 0; a < tab[x].size(); a++) {
          in[tab[x][a]]--;
          if (in[tab[x][a]] == 0)
            secik.insert(make_pair(c[tab[x][a]], tab[x][a]));
        }
        secik.erase(*it);
      } else
        cur = 0;
    }
  }
  int ans = 0;
  if (t[0] == 1) ans = 1;
  for (int a = 1; a < t.size(); a++) {
    if (t[a] != t[a - 1] && t[a] == 1) ans++;
  }
  cout << ans << endl;
  return 0;
}
