#include<bits/stdc++.h>
using namespace std;
#define rd(x) scanf("%d",&x)
#define rdd(x,y) scanf("%d%d",&x,&y)
#define rddd(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rds(s) scanf("%s",s)
#define rep(i,n) for(int i=0;i<n;i++)
#define LL long long
const int N = 1e5+10;
const int M=5e5+10;
const long long inf=1e15;
const int MOD=1e9+7;
int n,m,k;
int t;
int tot;
int head[N*2];
struct Edge{
int to,nxt,w;
}edge[M*6];
void addEdge(int u,int v,int w){
edge[tot].to=v;
edge[tot].w=w;
edge[tot].nxt=head[u];
head[u]=tot++;
}
map<string,int> mapp;
LL dis[N*2];
bool vis[N*2];
queue<int> que;
LL ans;
void solve(int s,int e){
ans=inf;
memset(vis,0,sizeof vis);
for(int i=1;i<=n*2;i++) dis[i]=inf;
dis[s]=0;
while(!que.empty()) que.pop();
que.push(s);vis[s]=1;
while(!que.empty()){
int u=que.front();que.pop();vis[u]=0;
if(u==e) {
if(dis[u]<ans) ans=dis[u];
continue;
}
for(int k=head[u];~k;k=edge[k].nxt){
int v=edge[k].to,w=edge[k].w;
if(w+dis[u]<dis[v]) {
dis[v]=dis[u]+w;
if(!vis[v]){
que.push(v);
vis[v]=1;
}
}
}
}
if(dis[e]<inf) printf("%I64d\n",dis[e]);
else puts("-1");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aaa","r",stdin);
#endif
int T,q;
string s3,s4;
char s1[20],s2[20];
int w;
while(~scanf("%d%d",&n,&m)){
mapp.clear();t=0;
memset(head,-1,sizeof head);
tot=0;
rep(i,m){
scanf("%s %s %d",s1,s2,&w);s3=s1;s4=s2;
if(mapp.find(s3)==mapp.end()) mapp[s3]=++t;
if(mapp.find(s4)==mapp.end()) mapp[s4]=++t;
int u=mapp[s3],v=mapp[s4];
addEdge(u,v,w);
addEdge(u,v+n,w/2);
addEdge(u+n,v+n,w);
}
scanf("%s%s",s1,s2);s3=s1;s4=s2;
int s=-1,e=-1;
if(mapp.find(s3)!=mapp.end()) s=mapp[s3];
if(mapp.find(s4)!=mapp.end()) e=mapp[s4];
if(s==-1 || e==-1) puts("-1");
else{
if(s==e){
puts("0");
}else
solve(s,e+n);
}
}
return 0;
}