#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000 + 5;
int ch[MAXN];
vector<int> edge[MAXN];
bool vis[MAXN];
int dp[MAXN][26];
bool dfs(int u) {
  if (vis[u]) return 0;
  if (dp[u][ch[u]] > 0) return 1;
  vis[u] = 1;
  dp[u][ch[u]] = 1;
  for (int i = 0; i < edge[u].size(); i++) {
    int v = edge[u][i];
    if (!dfs(v)) return 0;
    for (int c = 0; c < 26; c++) {
      if (c == ch[u])
        dp[u][c] = max(dp[u][c], dp[v][c] + 1);
      else
        dp[u][c] = max(dp[u][c], dp[v][c]);
    }
  }
  vis[u] = 0;
  return 1;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  getchar();
  for (int i = 1; i <= n; i++) {
    ch[i] = getchar() - 'a';
  }
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!dfs(i)) {
      printf("-1\n");
      return 0;
    }
    for (int j = 0; j < 26; j++) {
      ans = max(ans, dp[i][j]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
