#include using std::cin; using std::cout; using std::endl; using ll = long long; using std::vector; using std::string; #define rep(i, j, n) for(int i = int(j); i < int(n); i++) #define REP(i, j, n) for(int i = int(j); i <= int(n); i++) #define per(i, j, n) for(int i = int(j); int(n) <= i; i--) ll N, M; string S; vector> edge, dp, rev; vector> c; vector flag; ll ans = 0; void dfs(ll now){ flag[now] = true; rep(i,0,edge[now].size()){ ll next = edge[now][i]; ll ch = c[now][i]; if(flag[next]) continue; dfs(next); per(j,M,1){ dp[next][j] = std::max(dp[next][j], dp[next][j-1]+(S[j-1]==ch)); } REP(j,1,M){ dp[next][j] = std::max(dp[next][j], dp[next][j-1]); } REP(j,1,M){ rev[next][j-1] = std::max(rev[next][j-1], rev[next][j]+(S[j-1]==ch)); } per(j,M,1){ rev[next][j-1] = std::max(rev[next][j-1], rev[next][j]); } REP(j,0,M){ ans = std::max(ans, dp[now][j]+rev[next][j]); ans = std::max(ans, dp[next][j]+rev[now][j]); } REP(j,0,M){ dp[now][j] = std::max(dp[now][j], dp[next][j]); rev[now][j] = std::max(rev[now][j], rev[next][j]); } } } int main(){ ll max = 2000; cin >> N >> S; assert(N<=max && 2<=N && 1<=S.size() && S.size()<=max); rep(i,0,S.size()){ assert('a'<=S[i] && S[i]<='z'); } M = S.size(); edge.resize(N); c.resize(N); rep(i,1,N){ ll u, v; char c_; cin >> u >> v >> c_; assert(1<=u && u(M+1)); rev.resize(N, vector(M+1)); flag.resize(N); dfs(0); cout << ans << endl; rep(i,0,N) assert(flag[i]); }