#include <bits/stdc++.h>
using namespace std;
set<long long> adj[300005];
long long color[300005], visited[300005];
string s;
long long dp[300005][26];
bool cycle = false;
void iscycle(long long u, long long p) {
  color[u] = 1;
  for (auto v : adj[u]) {
    if (color[v] == 0) {
      iscycle(v, u);
    } else if (color[v] == 1) {
      cycle = true;
      return;
    }
  }
  color[u] = 2;
}
void dfs(long long u, long long p) {
  visited[u] = 1;
  for (auto v : adj[u]) {
    if (!visited[v]) {
      dfs(v, u);
    }
    for (long long i = 0; i < 26; i++) {
      dp[u][i] = max(dp[u][i], dp[v][i]);
    }
  }
  dp[u][s[u - 1] - 97] += 1;
}
long long indegree[300005];
void solve() {
  long long n, m;
  cin >> n >> m;
  cin >> s;
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    adj[a].insert(b);
    indegree[b]++;
  }
  long long start;
  for (long long i = 1; i <= n; i++) {
    if (color[i] == 0) {
      iscycle(i, -1);
      if (cycle) {
        cout << -1 << "\n";
        return;
      }
    }
    if (indegree[i] == 0) {
      start = i;
      dfs(start, -1);
    }
  }
  long long mx = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < 26; j++) {
      mx = max(mx, dp[i][j]);
    }
  }
  cout << mx << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  while (t--) solve();
}
