#include <bits/stdc++.h>
using namespace std;
template <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;
using ld = long double;
using ll = long long;
const int inf = 1e9, mod = 1e9 + 7;
const ll INF = 9e18;
const ld EPS = 1e-9, PI = acosl(-1.0);
const int N = 3e5 + 5;
char c[N];
int n, m, vis[N], ans = 0, val[N][26] = {};
set<P<int, int>> s;
V<int> vec;
V<V<int>> g(N);
void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    if (vis[v] == 1) {
      ans = -1;
      return;
    }
    if (!vis[v]) dfs(v);
  }
  vec.push_back(u);
  vis[u] = 2;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int x, y;
  cin >> n >> m;
  for (int i = 0, _i = (n); i < _i; ++i) cin >> c[i + 1];
  for (int i = 0, _i = (m); i < _i; ++i) {
    cin >> x >> y;
    if (s.find(make_pair(x, y)) == s.end()) {
      s.insert(make_pair(x, y));
      g[x].push_back(y);
    }
    if (x == y) {
      cout << -1;
      return 0;
    }
  }
  for (int i = (1), _i = (n + 1); i < _i; ++i) {
    if (!vis[i]) dfs(i);
  }
  if (ans == -1) {
    cout << -1;
    return 0;
  }
  for (int u : vec) {
    for (int v : g[u]) {
      for (int i = 0, _i = (26); i < _i; ++i) {
        val[u][i] = max(val[u][i], val[v][i]);
      }
    }
    val[u][c[u] - 'a']++;
    ans = max(ans, val[u][c[u] - 'a']);
  }
  cout << ans;
}
