#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n1, long long n2) {
  if (!n1) return n2;
  if (!n2) return n1;
  if (n1 % n2 == 0) return n2;
  return gcd(n2, n1 % n2);
}
long long powmod(long long base, long long exponent) {
  base %= 1000000007;
  long long ans = 1;
  while (exponent) {
    if (exponent & 1) ans = (ans * base) % 1000000007;
    base = (base * base) % 1000000007;
    exponent /= 2;
  }
  ans %= 1000000007;
  return ans;
}
int arr[1000100 + 1];
vector<int> adj[1000100 + 1];
vector<int> rev[1000100 + 1];
int dp[1000100 + 1];
bool visited[1000100 + 1];
void dfs(int node) {
  visited[node] = true;
  for (int i = 0; i < adj[node].size(); i++)
    if (!visited[adj[node][i]]) dfs(adj[node][i]);
  int mx = arr[node];
  for (int i = 0; i < adj[node].size(); i++) {
    int child = adj[node][i];
    if (arr[child] || !arr[node])
      mx = max(mx, dp[child]);
    else
      mx = max(mx, dp[child] + 1);
  }
  dp[node] = mx;
}
int main() {
  int n, i, j, k, m, a, b;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 1; i <= n; i++) scanf("%d", &arr[i]);
  for (i = 1; i <= m; i++) {
    scanf("%d", &a);
    scanf("%d", &b);
    a++;
    b++;
    adj[a].push_back(b);
    rev[b].push_back(a);
  }
  int answer = 0;
  for (i = 1; i <= n; i++) {
    int temp = rev[i].size();
    if (temp == 0) dfs(i);
  }
  for (i = 1; i <= n; i++) {
    answer = max(answer, dp[i]);
  }
  printf("%d\n", answer);
  return 0;
}
