#include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 7;
const long double eps = 1e-9;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
  if (a < 0) return -1;
  if (a > 0) return 1;
  return 0;
}
template <typename T>
inline void in(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
long long twop(int x) { return 1LL << x; }
template <typename A, typename B>
inline void in(A &x, B &y) {
  in(x);
  in(y);
}
template <typename A, typename B, typename C>
inline void in(A &x, B &y, C &z) {
  in(x);
  in(y);
  in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
  in(x);
  in(y);
  in(z);
  in(d);
}
template <class T>
void upd(T &a, T b) {
  a = max(a, b);
}
const int maxn = 100010;
vector<int> g[maxn];
bool onCo[maxn];
int n, m;
int deg[maxn];
int ans[maxn];
void topo() {
  vector<int> ord;
  deque<int> q;
  for (long long i = 0; i < n; i++)
    if (deg[i] == 0) {
      if (onCo[i])
        q.push_back(i);
      else
        q.push_front(i);
    }
  int ans = 0;
  bool lastBlack = true;
  while (((int)q.size())) {
    int f = q.front();
    q.pop_front();
    if (onCo[f] && lastBlack) ans++;
    if (!onCo[f])
      lastBlack = true;
    else
      lastBlack = false;
    for (auto nxt : g[f]) {
      deg[nxt]--;
      if (!deg[nxt]) {
        if (onCo[nxt] == onCo[f])
          q.push_front(nxt);
        else
          q.push_back(nxt);
      }
    }
  }
  cout << ans << endl;
}
int main() {
  in(n, m);
  for (long long i = 0; i < n; i++) {
    int c;
    in(c);
    if (c) onCo[i] = true;
  }
  for (long long i = 0; i < m; i++) {
    int u, v;
    in(u, v);
    deg[u]++;
    g[v].push_back(u);
  }
  topo();
  return 0;
}
