#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
string s;
vector<int> flag;
vector<vector<int>> cnt;
Graph g;
enum { UNCHECK, CHECKING, CHECKED };
void dfs(int v) {
  if (flag[v] == CHECKING) throw "CIRCUIT";
  if (flag[v] == CHECKED) return;
  flag[v] = CHECKING;
  for (int i = 0; i < g[v].size(); i++) {
    dfs(g[v][i]);
    for (int j = 0; j <= 'z' - 'a'; j++) {
      cnt[v][j] = max(cnt[v][j], cnt[g[v][i]][j]);
    }
  }
  cnt[v][s[v] - 'a']++;
  flag[v] = CHECKED;
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  cin >> s;
  g.assign(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
  }
  flag.assign(n, UNCHECK);
  cnt.assign(n, vector<int>('z' - 'a' + 1, 0));
  int res = 0;
  for (int i = 0; i < n; i++) {
    try {
      dfs(i);
      res = max(res, *max_element(cnt[i].begin(), cnt[i].end()));
    } catch (const char* msg) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;
  return 0;
}
