#include <bits/stdc++.h>
using namespace std;
template <class x>
x gcd(x a, x b) {
  return (b == 0) ? a : gcd(b, a % b);
}
template <class x>
x powerm(x a, x n) {
  if (n == 0) return 1;
  x b = powerm(a, n / 2) % 1000000007;
  return (n & 1) ? ((b * b) % 1000000007 * a) % 1000000007
                 : (b * b) % 1000000007;
}
const long long cnst = 1e6 + 5;
vector<vector<int> > g;
string s;
vector<bool> vis;
vector<vector<int> > dp;
int ans;
vector<int> dfs(int i) {
  if (dp[i].size()) return dp[i];
  vis[i] = 1;
  vector<int> ans(26, 0);
  for (int ch : g[i]) {
    if (vis[ch] == 0) {
      vector<int> tmp = dfs(ch);
      for (int j = 0; j <= 25; j++) ans[j] = max(ans[j], tmp[j]);
    } else {
      cout << -1 << "\n";
      exit(0);
    }
  }
  ans[s[i] - 'a']++;
  vis[i] = 0;
  return dp[i] = ans;
}
void solve() {
  int n, m;
  ans = 0;
  cin >> n >> m;
  g.resize(n + 1, vector<int>());
  dp.resize(n + 1, vector<int>());
  vis.resize(n + 1, 0);
  cin >> s;
  s = '.' + s;
  int x, y;
  for (int i = 1; i <= m; i++) cin >> x >> y, g[x].push_back(y);
  for (int i = 1; i <= n; i++) {
    dfs(i);
    ans = max(ans, *max_element(dp[i].begin(), dp[i].end()));
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  int t = 1;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
}
