#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int n, m, dp[100005], a[100005];
vector<int> gr[100005];
bool vis[100005];
void dfs(int x) {
  dp[x] += a[x];
  vis[x] = true;
  for (int i = 0; i < gr[x].size(); i++) {
    int u = gr[x][i];
    if (vis[u] == 0) dfs(u);
    if (a[x] == 1 && a[u] == 0)
      dp[x] = max(dp[x], dp[u] + 1);
    else
      dp[x] = max(dp[x], dp[u]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    gr[y].push_back(x);
  }
  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) dfs(i);
    mx = max(mx, dp[i]);
  }
  cout << mx;
}
