#include <iostream>
#include <map>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <functional>

using namespace std;

const int INF = 1<<28;

struct edge_t {
    int from, to, cost;
    edge_t(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    bool operator<(const edge_t &o) const {
        return (cost < o.cost);
    }
    bool operator>(const edge_t &o) const {
        return (cost > o.cost);
    }
};
struct state_t {
    int v;
    int min_cost;
    state_t(int v, int min_cost) : v(v), min_cost(min_cost) {}
    bool operator<(const state_t &o) const {
        return (min_cost < o.min_cost);
    }
    bool operator>(const state_t &o) const {
        return (min_cost > o.min_cost);
    }
};
typedef vector<edge_t> vertex_t;
struct graph_t {
    vector<vertex_t> g;

    graph_t() {}

    graph_t(int vertex_count) {
        g.resize(vertex_count);
    }
    graph_t(vector<vertex_t> g) : g(g) {}

    inline vertex_t& operator[](int index) {
        return g[index];
    }

    inline void push_back(vertex_t v) {
        g.push_back(v);
    }

   void dijkstra(int s, vector<int> &min_cost) {
        min_cost.resize(g.size());
        priority_queue< state_t, vector<state_t>, greater<state_t> > q;
        
        fill(min_cost.begin(), min_cost.end(), INF);

        min_cost[s] = 0;
        q.push(state_t(s, 0));

        while (!q.empty()) {
            state_t p = q.top(); q.pop();
            int v = p.v;
            if (min_cost[v] < p.min_cost) continue;
            for (int i = 0; i < g[v].size(); i++) {
                edge_t e = g[v][i];
                if (min_cost[e.to] > min_cost[v] + e.cost)  {
                    min_cost[e.to] = min_cost[v] + e.cost;
                    q.push(state_t(e.to, min_cost[e.to]));
                }
            }
        }
    }
};

bool has_key(map<string, int> &m, string key) { 
    if (m.end() != m.find(key)) return true;
    return false;
}

int main() {
    int n, m;
    string s, p, g;

    while (cin >> n >> m) {
        if (n == 0 && m == 0) break;
        graph_t graph(n);
        map<string, int> symbol;
        cin >> s >> p >> g;
        symbol[s] = 0;
        symbol[p] = 1;
        symbol[g] = 2;
        int next_symbol = 3;
//        for (map<string, int>::iterator it = symbol.begin(); it != symbol.end(); it++) {
//            cout << "key: " << it->first << " value: " << it->second << endl;
//        }
        
        string a, b;
        int cost, delay;
        for (int i = 0; i < m; i++) {
            cin >> a >> b >> cost >> delay;
            if (!has_key(symbol, a)) 
                symbol[a] = next_symbol++;
            if (!has_key(symbol, b)) 
                symbol[b] = next_symbol++;
//            for (map<string, int>::iterator it = symbol.begin(); it != symbol.end(); it++) {
//                cout << "key: " << it->first << " value: " << it->second << endl;
//            }
            graph[symbol[a]].push_back(edge_t(symbol[a], symbol[b], cost / 40 + delay));
            graph[symbol[b]].push_back(edge_t(symbol[b], symbol[a], cost / 40 + delay));
        }

        int total_time = 0;
        vector<int> min_cost;
        graph.dijkstra(symbol[s], min_cost);
        total_time += min_cost[symbol[p]];
        graph.dijkstra(symbol[p], min_cost);
        total_time += min_cost[symbol[g]];

        cout << total_time << endl;
    }
    
    return 0;
}
            

Measured memory_bytes: 1100