#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }
template<class T>
ostream &operator<<(ostream &os,const vector<T> &v){
	for(int i = 0;i < v.size();i++) os << (i ? "," : "[") << v[i];
	os << "]";
	return os;
}

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }

const int INF=1e+18;
const double EPS=1e-9;
const int MOD=998244353;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

template<class T>
struct Edge{
	int from,to;
	T cost;
	Edge(int to,T cost) : to(to),cost(cost){}
	Edge(int from,int to,T cost) : from(from),to(to),cost(cost){}
	operator int() const noexcept { return to; }
};

template<class T>
ostream &operator<<(ostream &os,const Edge<T> &p){ return os << "(" << p.to << "," << p.cost << ")"; }

template<class T>
using WeightedGraph = vector<vector<Edge<T>>>;
using Graph = vector<vector<int>>;
template<class T>
using Matrix = vector<vector<T>>;

template<class T>
vector<T> dijkstra(const WeightedGraph<T> &G,int source = 0){
	using P = pair<T,int>;
	vector<T> dist(G.size(),INF);
	priority_queue<P,vector<P>,greater<P>> que;
	dist[source] = 0;
	que.emplace(dist[source],source);
	while(!que.empty()){
		P p = que.top();que.pop();
		int v = p.second;
		if(dist[v] < p.first) continue;
		for(const auto &e : G[v]){
			if(dist[e.to] > dist[v] + e.cost){
				dist[e.to] = dist[v] + e.cost;
				que.emplace(dist[e.to],e.to);
			}
		}
	}
	return dist;
}

int n,m;

void solve(){
	int cap,v = 0,c1[3010],c2[3010],d[3010];
	string s,t;
	map<string,int> mp;
	vector<int> lpg = {0,1};
	cin >> cap >> s >> t;
	mp[s] = v++; mp[t] = v++;
	for(int i = 0;i < n;i++){
		string sc1,sc2;
		cin >> sc1 >> sc2 >> d[i];
		if(!mp.count(sc1)) mp[sc1] = v++;
		if(!mp.count(sc2)) mp[sc2] = v++;
		c1[i] = mp[sc1];
		c2[i] = mp[sc2];
	}
	for(int i = 0;i < m;i++){
		string ss;
		cin >> ss;
		if(ss != s && ss != t) lpg.push_back(mp[ss]);
	}
	WeightedGraph<int> G(mp.size()),lG(lpg.size());
	for(int i = 0;i < n;i++){
		G[c1[i]].emplace_back(c2[i],d[i]);
		G[c2[i]].emplace_back(c1[i],d[i]);
	}
	for(int i = 0;i < lpg.size();i++){
		auto dist = dijkstra(G,lpg[i]);
		for(int j = i + 1;j < lpg.size();j++){
			if(dist[lpg[j]] <= cap * 10){
				lG[i].emplace_back(j,dist[lpg[j]]);
				lG[j].emplace_back(i,dist[lpg[j]]);
			}
		}
	}
	auto dist = dijkstra(lG,0);
	if(dist[1] == INF) cout << -1 << endl;
	else cout << dist[1] << endl;
}

signed main(){
	while(cin >> n >> m,n) solve();
}
