#include <bits/stdc++.h>
using namespace std;
int const lmt = 3 * 1e5 + 2;
string s;
int n, m;
int ans = 0;
vector<int> adj[lmt];
int arr[lmt][26];
bool vis[lmt];
bool deg[lmt];
int in[lmt];
int dd[lmt];
void dfs(int p) {
  vis[p] = true;
  arr[p][s[p - 1] - 'a']++;
  for (int i = 0; i < adj[p].size(); i++) {
    int c = adj[p][i];
    if (!vis[c]) dfs(c);
    for (int j = 0; j < 26; j++) {
      if (j == s[p - 1] - 'a') {
        arr[p][j] = max(arr[p][j], arr[c][j] + 1);
      } else
        arr[p][j] = max(arr[p][j], arr[c][j]);
      ans = max(ans, arr[p][j]);
    }
  }
}
bool cy = false;
void cycle(int p) {
  if (cy) return;
  dd[p] = 1;
  for (int i = 0; i < adj[p].size(); i++) {
    int c = adj[p][i];
    if (dd[c] == 1) {
      cy = true;
      break;
    } else if (dd[c] == 0)
      cycle(c);
  }
  dd[p] = 2;
}
int main() {
  cin >> n >> m;
  cin >> s;
  int a, b;
  memset(deg, false, sizeof deg);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    deg[b] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (dd[i] == 0) {
      cycle(i);
      if (cy) {
        ans = -1;
        break;
      }
    }
  }
  memset(vis, false, sizeof vis);
  for (int i = 1; i <= n; i++) {
    if (!deg[i] && ans != -1) dfs(i);
  }
  cout << ans << "\n";
  return 0;
}
