#include <iostream>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <algorithm>
#include <stack>
#include <bitset>
#define pb push_back
using namespace std;
/*void compile(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}*/
int main(){
compile();
int t = 1;
//cin >> t;
while(t--){
int n, m;
cin >> n >> m;
map<int,vector<pair<int,int>>>mp;
for(int i = 0 ; i < m ; i++){
int u, vv, w;
scanf("%d%d%d",&u, &vv, &w);
mp[u].pb(make_pair(vv,w));
mp[vv].pb(make_pair(u, w));
}
string s;
cin >> s;
bool f = 0;
long long ans = INT_MAX;
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j < mp[i].size() ; j++){
if(s[mp[i][j].first - 1] == s[i - 1]){
ans = min((int)ans, mp[i][j].second);
f = 1;
}
int num = mp[i][j].first;
vector<int>temp;
for(int k = 0 ; k < mp[num].size() ; k++){
if(s[mp[num][k].first - 1] == s[i - 1] && mp[num][k].first != i){
temp.pb(mp[num][k].second);
}
}
if(temp.size() > 0){
f = 1;
for(int l = 0 ; l < temp.size() ; l++){
//cout<<temp[l]<<" "<<mp[i][j].second<<endl;
ans = min(ans, (long long)temp[l] + mp[i][j].second);
}
}
temp.clear();
}
}
if(f)
cout<<ans<<endl;
else puts("-1");
}
}
Copy