#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005],dp[200005];
int to[200005],n,m,CLK,RES;

int main()
{
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  vector<int> s(n+1);
  for(int i=1; i<=n; i++)
    cin >> s[i];
  for(int x,y; m--; v[x].push_back(y),++to[y])
    cin>>x>>y;
  queue<int> q;
  for(int i=1; i<=n; i++) 
  {
    dp[i].resize(51);
    if(!to[i]) q.push(i);
  }
  for(;!q.empty();++CLK){
    int u = q.front();
    ++dp[u][s[u]];
    q.pop();
    RES = max(RES,*max_element(dp[u].begin(), dp[u].end()));
    for(auto i:v[u])
    {
      for(int j=1;j<=50;++j)
        dp[i][j]=max(dp[i][j],dp[u][j]);
      if(!--to[i]) q.push(i);
    }
  }
  if(CLK != n)
    cout << "NIE\n";
  else
    cout << RES << '\n';
  return 0;
}
