#include<bits/stdc++.h>
using namespace std;
const int MAXN=202;
const int INF=1000000000;
int weight[MAXN]={};
map<string,int> sti;
map<int,string> its;
int G[MAXN][MAXN];
int d[MAXN];
int n,k;
int maxhap=-1,num;
double maxavg=0;
vector<int> pre[MAXN];
vector<int> path,temppath;
bool vis[MAXN]={false};
void dij(int v){
fill(d,d+MAXN,INF);
d[v]=0;
for(int i=0;i<n;i++){
int k=-1;int MIN=INF;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
k=j;MIN=d[j];
}
}
if(k==-1) return;
vis[k]=true;
for(int j=0;j<n;j++){
if(vis[j]==false&&G[k][j]!=INF){
if(d[k]+G[k][j]<d[j]){
d[j]=d[k]+G[k][j];
pre[j].clear();
pre[j].push_back(k);
}else if(d[k]+G[k][j]==d[j]){
pre[j].push_back(k);
}
}
}
}
}
void dfs(int ed){
if(ed==0){
temppath.push_back(ed);num++;
int temphap=0;double tempavg=0;
for(int i=temppath.size()-1;i>=0;i--){
temphap+=weight[temppath[i]];
}
tempavg=1.0*temphap/(temppath.size()-1);
if(temphap>maxhap){
maxhap=temphap;
maxavg=tempavg;
path=temppath;
}else if(temphap==maxhap&&maxavg<tempavg){
maxavg=tempavg;
path=temppath;
}
temppath.pop_back();
return;
}
temppath.push_back(ed);
for(int i=0;i<pre[ed].size();i++){
dfs(pre[ed][i]);
}
temppath.pop_back();
}
int main()
{
//freopen("in.txt","r",stdin);
cin>>n>>k;
string start;cin>>start;
int j=0;
sti[start]=j;weight[sti[start]]=0;
its[j]=start;j++;
for(int i=0;i<n-1;i++){
string temp;int t;
cin>>temp>>t;
sti[temp]=j;its[j]=temp;j++;
weight[sti[temp]]=t;
}
fill(G[0],G[0]+MAXN*MAXN,INF);
for(int i=0;i<k;i++){
string _start,end;int cos;
cin>>_start>>end>>cos;
int t1=sti[_start];
int t2=sti[end];
G[t1][t2]=cos;G[t2][t1]=cos;
}
dij(0);
dfs(sti["ROM"]);
cout<<num<<' '<<d[sti["ROM"]]<<' '<<maxhap<<' '<<(int)maxavg<<endl;
for(int i=path.size()-1;i>=0;i--){
if(i==path.size()-1) cout<<its[path[i]];
else cout<<"->"<<its[path[i]];
}
return 0;
}