#include <bits/stdc++.h>
using namespace std;
int n, m, u, v;
int a[100005];
vector<int> e[100005]; // 邻接表
int ind[100005]; // 拓扑排序 入度
int dp[100005][15]; // 状态:到达i节点,末尾为j的最长长度
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", a + i);
for (int i = 1; i <= m; i++)
{
scanf("%d %d", &u, &v);
e[u].push_back(v);
ind[v]++;
}
// 拓扑排序
queue<int> q;
for (int i = 1; i <= n; i++)
{
if (ind[i] == 0)
q.push(i);
dp[i][a[i]] = 1;
}
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = 0; i < e[u].size(); i++)
{
int v = e[u][i];
ind[v]--;
if (ind[v] == 0)
q.push(v);
// 状态转移
for (int i = 1; i <= a[v]; i++)
if (dp[u][i] + 1 > dp[v][a[v]])
dp[v][a[v]] = dp[u][i] + 1;
for (int i = 1; i <= 10; i++)
if (dp[u][i] > dp[v][i])
dp[v][i] = dp[u][i];
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 10; j++)
ans = max(ans, dp[i][j]);
cout << ans;
return 0;
}