#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int inf=2100000000;
int N,K;
string st;
int ed;
map<string,int> mp1;
map<int,string> mp2;
int g[1050][1050];//花费
int w[1050]={0};
int pre[1050];
int num[1050]={0};
int cou[1050]={0};
int wei[1050]={0};
int dis[1050];
int book[1050]={0};
void dijk(int st)
{
dis[st]=0;
book[st]=1;
num[st]=1;//******************!!!!!!
for(int i=1;i<=N-1;i++)
{
if(g[st][i]!=inf)
{
dis[i]=g[st][i];
wei[i]=w[i];
num[i]=1;//*******!!!!!
cou[i]=1;
}
}
for(int i=0;i<N-1;i++)
{
int u=-1,minn=inf;
for(int j=1;j<=N-1;j++)
{
if(book[j]==0&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
if(u==-1)
return;
book[u]=1;
for(int v=0;v<=N-1;v++)
{
if(book[v]==0&&g[u][v]!=inf)
{
if(dis[u]+g[u][v]<dis[v])
{
dis[v]=dis[u]+g[u][v];
wei[v]=wei[u]+w[v];
cou[v]=cou[u]+1;
pre[v]=u;
num[v]=num[u];
}
else if(dis[u]+g[u][v]==dis[v])
{
num[v]+=num[u];//****************!!!!
if(wei[u]+w[v]>wei[v])
{
dis[v]=dis[u]+g[u][v];
wei[v]=wei[u]+w[v];
cou[v]=cou[u]+1;
pre[v]=u;
}
else if(wei[u]+w[v]==wei[v]&&cou[u]+1<cou[v])
{
dis[v]=dis[u]+g[u][v];
wei[v]=wei[u]+w[v];
cou[v]=cou[u]+1;
pre[v]=u;
}
}
}
}
}
}
void init()
{
fill(g[0],g[0]+1050*1050,inf);
fill(dis,dis+1050,inf);
memset(pre,-1,sizeof(pre));
}
//int main()
{
freopen("in.txt","r",stdin);
cin>>N>>K>>st;
init();
mp1[st]=0;
mp2[0]=st;
for(int i=1;i<=N-1;i++)
{
string ts;
int t;
cin>>ts>>t;
mp1[ts]=i;
mp2[i]=ts;
w[i]=t;
if(ts=="ROM")
ed=i;
}
for(int i=0;i<K;i++)
{
string ts1,ts2;
int t;
cin>>ts1>>ts2>>t;
int t1=mp1[ts1];
int t2=mp1[ts2];
g[t1][t2]=t;
g[t2][t1]=t;
}
dijk(0);
vector<int> v;
int p=ed;
while(p!=-1)
{
v.push_back(p);
p=pre[p];
}
printf("%d %d %d %d\n",num[ed],dis[ed],wei[ed],wei[ed]/cou[ed]);
cout<<mp2[0]<<"->";
for(int i=v.size()-1;i>=1;i--)
cout<<mp2[v[i]]<<"->";
cout<<"ROM";
return 0;
}