#include <bits/stdc++.h>
using namespace std;
vector<int> vp[300005];
int visited[300005];
int cnt[300005][26];
int n, m, x, y, ans = 0;
string s;
void dfs(int u) {
  visited[u] = 1;
  for (int i = 0; i < vp[u].size(); i++) {
    int v = vp[u][i];
    if (!visited[v]) dfs(v);
    if (visited[v] == 1) {
      cout << -1 << endl;
      exit(0);
    }
    for (int j = 0; j < 26; j++) {
      cnt[u][j] = max(cnt[u][j], cnt[v][j]);
    }
  }
  cnt[u][s[u - 1] - 'a']++;
  for (int i = 0; i < 26; i++) {
    ans = max(ans, cnt[u][i]);
  }
  visited[u] = 2;
}
int main() {
  cin >> n >> m;
  cin >> s;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    vp[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) dfs(i);
  }
  cout << ans << endl;
  return 0;
}
