#include <bits/stdc++.h>
using namespace std;
const int SZ = 1e5 + 10;
vector<vector<long long int> > g;
vector<long long int> e, col, order;
void dfs(long long int x) {
  col[x] = 1;
  for (auto y : g[x]) {
    if (!col[y]) dfs(y);
  }
  order.push_back(x);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long int mn, mx;
  long long int n, m, t, k, i, j, sum = 0, flag = 0, cnt = 0;
  long long int x = 0, y = 0, z, l, r, q;
  int TC = 1;
  while (TC--) {
    cin >> n >> m;
    e.resize(n);
    g.resize(n);
    col.assign(n, 0);
    for (i = 0; i < n; ++i) cin >> e[i];
    vector<vector<long long int> > r(n);
    for (i = 0; i < m; ++i) {
      cin >> x >> y;
      g[y].push_back(x);
      r[x].push_back(y);
    }
    for (i = 0; i < n; ++i) {
      if (col[i] == 0) {
        dfs(i);
      }
    }
    mx = 0;
    col.assign(n, 0);
    reverse((order).begin(), (order).end());
    vector<long long int> dp(n);
    for (auto x : order) {
      if ((long long int)r[x].size() == 0) {
        dp[x] = (e[x] == 1);
      } else {
        for (auto y : r[x]) {
          dp[x] = max(dp[x], dp[y] + (e[x] == 1 and e[y] == 0));
        }
      }
    }
    mx = 0;
    for (i = 0; i < n; ++i) mx = max(mx, dp[i]);
    cout << mx << '\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
  return 0;
}
