#include using namespace std; // #include // using namespace atcoder; // #include // using namespace boost::multiprecision; #define ll long long #define ld long double #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define vi vector #define vl vector #define vd vector #define vb vector #define vs vector #define vc vector #define ull unsigned long long #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() template inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } // #define ll int // #define ll int128_t // #define ll int256_t // #define ll cpp_int constexpr ll inf = (1ll << 61); // constexpr ll inf = (1 << 30); // const double PI=3.1415926535897932384626433832795028841971; // ll rui(ll a,ll b){ // if(b==0)return 1; // if(b%2==1) return a*rui(a*a,b/2); // return rui(a*a,b/2); // } // vl fact; // ll kai(ll n){ // fact.resize(n,1); // rep(i,n-1)fact[i+1]=fact[i]*(i+1); // } // using mint = ld; // using mint = modint998244353;//static_modint<998244353> // using mint = modint1000000007;//static_modint<1000000007> // using mint = static_modint<922267487>; // 多分落とされにくい NOT ntt-friendly // using mint = static_modint<469762049>; // ntt-friendly // using mint = static_modint<167772161>; // ntt-friendly // using mint = modint;//mint::set_mod(mod); // ll const mod=1000000007ll; // ll const mod=998244353ll; // ll modrui(ll a,ll b,ll mod){ // a%=mod; // if(b==0)return 1; // if(b%2==1) return a*modrui(a*a%mod,b/2,mod)%mod; // return modrui(a*a%mod,b/2,mod)%mod; // } // void incr(vl &v,ll n){// n進法 // ll k=v.size(); // v[k-1]++; // ll now=k-1; // while (v[now]>=n) // { // v[now]=0; // if(now==0)break; // v[now-1]++; // now--; // } // return; // } // vector fact,invf; // void init_modfact(ll sz){ // fact.resize(sz); // invf.resize(sz); // fact[0]=1; // rep(i,sz-1){ // fact[i+1]=fact[i]*(i+1); // } // invf[sz-1]=1/fact[sz-1]; // for(ll i=sz-2; i>=0; i--){ // invf[i]=invf[i+1]*(i+1); // } // } // mint choose(ll n,ll r){ // if(n modpow,invpow; // void init_modpow(ll x,ll sz){ // mint inv=1/mint(x); // modpow.assign(sz,1); // invpow.assign(sz,1); // rep(i,sz-1){ // modpow[i+1]=modpow[i]*x; // invpow[i+1]=invpow[i]*inv; // } // } // long long phi(long long n) {// O(sqrt(n)) // long long res = n; // for (long long i = 2; i * i <= n; i++) { // if (n % i == 0) { // res -= res / i; // while (n % i == 0) n /= i; // } // } // if (n > 1) res -= res / n; // return res; // } struct edge{ ll to,cost; }; string kcpc="KCPC"; void solve(){ ll n,m; cin >> n >> m; vector> g(n); rep(i,m){ ll u,v,c; cin >> u >> v >> c; u--;v--; g[u].push_back(edge{v,c}); } string s; cin >> s; ll ans=inf; using A=array; priority_queue,greater> pq; vector dist0(n,vector>(4,{inf,-1})),dist1(n,vector>(4,{inf,-2})); dist0[0][s[0]=='K']={0,-1}; pq.push({0,0,s[0]=='K',-1}); while(!pq.empty()){ auto [d,x,k,cidx]=pq.top(); pq.pop(); if(!(dist1[x][k]==pair{d,cidx} || dist0[x][k]==pair{d,cidx}))continue; for(auto [to,cost]:g[x]){ ll nd=d+cost; { if(dist0[to][k].second==cidx){ if(chmin(dist0[to][k].first,nd)){ dist0[to][k].second=cidx; if(dist0[to][k]>dist1[to][k])swap(dist0[to][k],dist1[to][k]); pq.push({nd,to,k,cidx}); } } else if(chmin(dist1[to][k].first,nd)){ dist1[to][k].second=cidx; if(dist0[to][k]>dist1[to][k])swap(dist0[to][k],dist1[to][k]); pq.push({nd,to,k,cidx}); } } if(k!=3 && s[to]==kcpc[k]){ ll nc=cidx; if(k==1)nc=to; if(dist0[to][k+1].second==nc){ if(chmin(dist0[to][k+1].first,nd)){ dist0[to][k+1].second=nc; if(dist0[to][k+1]>dist1[to][k+1])swap(dist0[to][k+1],dist1[to][k+1]); pq.push({nd,to,k+1,nc}); } } else if(chmin(dist1[to][k+1].first,nd)){ dist1[to][k+1].second=nc; if(dist0[to][k+1]>dist1[to][k+1])swap(dist0[to][k+1],dist1[to][k+1]); pq.push({nd,to,k+1,nc}); } } if(k==3 && s[to]==kcpc[k]){ if(to!=cidx)chmin(ans,nd); } } } if(ans==inf)cout << -1 << endl; else cout << ans << endl; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); // ll mx=450; // vc fl(mx+1,0); // for(ll d=2;d<=mx;d++){ // if(fl[d])continue; // ll x=d; // ps.push_back(x); // while(x<=mx){ // fl[x]=1; // x+=d; // } // } ll t = 1; // cin >> t; while (t--){ solve(); } }