#include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define pi pair<int,int>
#define pb push_back
#define sec second
#define sh(i) (1LL<<i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int n,m,x,y,d[510][510];
string s,p,g;

int main(){
	while(cin>>n>>m,n){
		map<string,int> M;
		rep(i,n)rep(j,n)d[i+1][j+1]=(i!=j)*INF;
		cin>>s>>p>>g;
		M[s]=1,M[p]=2,M[g]=3;
		int cur=4;
		rep(i,m){
			cin>>s>>g>>x>>y;
			if(!M[s])M[s]=cur++;
			if(!M[g])M[g]=cur++;
			d[M[s]][M[g]]=d[M[g]][M[s]]=x/40+y;
		}
		rep2(k,1,n+1)rep2(i,1,n+1)rep2(j,1,n+1)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		cout<<d[1][2]+d[2][3]<<endl;
	}
}