Problem: Ken and Keiko are young, poor and busy. Short explanation: they are students, and ridden with part-time jobs. To make things worse, Ken lives in Hakodate and Keiko in Tokyo. They want to meet, but since they have neither time nor money, they have to go back to their respective jobs immediately after, and must be careful about transportation costs. Help them find the most economical meeting point.

Ken starts from Hakodate, Keiko from Tokyo. They know schedules and fares for all trains, and can choose to meet anywhere including their hometowns, but they cannot leave before 8am and must be back by 6pm in their respective towns. Train changes take no time (one can leave the same minute he/she arrives), but they want to meet for at least 30 minutes in the same city.

There can be up to 100 cities and 2000 direct connections, so you should devise an algorithm clever enough for the task.



Input

The input is a sequence of data sets.

The first line of a data set contains a single integer, the number of connections in the timetable. It is not greater than 2000.

Connections are given one on a line, in the following format.


Start_city HH:MM Arrival_city HH:MM price


Start_city and Arrival_city are composed of up to 16 alphabetical characters, with only the first one in upper case. Departure and arrival times are given in hours and minutes (two digits each, separated by ":") from 00:00 to 23:59. Arrival time is strictly after departure time. The price for one connection is an integer between 1 and 10000, inclusive. Fields are separated by spaces.

The end of the input is marked by a line containing a zero.

Output

The output should contain one integer for each data set, the lowest cost possible. This is the total fare of all connections they use.

If there is no solution to a data set, you should output a zero.

The solution to each data set should be given in a separate line.

Example

Input

5
Hakodate 08:15 Morioka 12:30 2500
Morioka 14:05 Hakodate 17:30 2500
Morioka 15:30 Hakodate 18:00 3000
Morioka 14:30 Tokyo 17:50 3000
Tokyo 08:30 Morioka 13:35 3000
4
Hakodate 08:15 Morioka 12:30 2500
Morioka 14:04 Hakodate 17:30 2500
Morioka 14:30 Tokyo 17:50 3000
Tokyo 08:30 Morioka 13:35 3000
18
Hakodate 09:55 Akita 10:53 3840
Hakodate 14:14 Akita 16:09 1920
Hakodate 18:36 Akita 19:33 3840
Hakodate 08:00 Morioka 08:53 3550
Hakodate 22:40 Morioka 23:34 3550
Akita 14:23 Tokyo 14:53 2010
Akita 20:36 Tokyo 21:06 2010
Akita 08:20 Hakodate 09:18 3840
Akita 13:56 Hakodate 14:54 3840
Akita 21:37 Hakodate 22:35 3840
Morioka 09:51 Tokyo 10:31 2660
Morioka 14:49 Tokyo 15:29 2660
Morioka 19:42 Tokyo 20:22 2660
Morioka 15:11 Hakodate 16:04 3550
Morioka 23:03 Hakodate 23:56 3550
Tokyo 09:44 Morioka 11:04 1330
Tokyo 21:54 Morioka 22:34 2660
Tokyo 11:34 Akita 12:04 2010
0


Output

11000
0
11090

Solution (in CPP):

Solution:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const int INF = INT_MAX / 8;

class Edge{
public:
    int to, start, arrival, cost;
    Edge(){
    }
    Edge(int to0, int start0, int arrival0, int cost0){
        to = to0;
        start = start0;
        arrival = arrival0;
        cost = cost0;
    }
};

vector<vector<Edge> > edges;
vector<vector<Edge> > revEdges;

void solve1(int startPos, vector<vector<int> >& minCost)
{
    int n = edges.size();
    minCost.assign(n, vector<int>(601, INF));
    multimap<int, pair<int, int> > mm;
    minCost[startPos][0] = 0;
    mm.insert(make_pair(0, make_pair(startPos, 0)));

    while(!mm.empty()){
        int cost = mm.begin()->first;
        int pos = mm.begin()->second.first;
        int time = mm.begin()->second.second;
        mm.erase(mm.begin());
        if(cost > minCost[pos][time])
            continue;

        for(unsigned i=0; i<edges[pos].size(); ++i){
            if(time <= edges[pos][i].start){
                int pos2 = edges[pos][i].to;
                int cost2 = cost + edges[pos][i].cost;
                int time2 = edges[pos][i].arrival;
                if(cost2 < minCost[pos2][time2]){
                    minCost[pos2][time2] = cost2;
                    mm.insert(make_pair(cost2, make_pair(pos2, time2)));
                }
            }
        }
    }
}

void solve2(int goalPos, vector<vector<int> >& minCost)
{
    int n = revEdges.size();
    minCost.assign(n, vector<int>(601, INF));
    multimap<int, pair<int, int> > mm;
    minCost[goalPos][600] = 0;
    mm.insert(make_pair(0, make_pair(goalPos, 600)));

    while(!mm.empty()){
        int cost = mm.begin()->first;
        int pos = mm.begin()->second.first;
        int time = mm.begin()->second.second;
        mm.erase(mm.begin());
        if(cost > minCost[pos][time])
            continue;

        for(unsigned i=0; i<revEdges[pos].size(); ++i){
            if(time >= revEdges[pos][i].arrival){
                int pos2 = revEdges[pos][i].to;
                int cost2 = cost + revEdges[pos][i].cost;
                int time2 = revEdges[pos][i].start;
                if(cost2 < minCost[pos2][time2]){
                    minCost[pos2][time2] = cost2;
                    mm.insert(make_pair(cost2, make_pair(pos2, time2)));
                }
            }
        }
    }
}

int main()
{
    for(;;){
        int m;
        cin >> m;
        if(m == 0)
            return 0;

        map<string, int> index;
        index["Hakodate"] = 0;
        index["Tokyo"] = 1;
        int n = 2;
        edges.assign(n, vector<Edge>(0));
        revEdges.assign(n, vector<Edge>(0));
        for(int i=0; i<m; ++i){
            string s1, s2;
            int h1, m1, h2, m2, cost;
            char c;
            cin >> s1 >> h1 >> c >> m1 >> s2 >> h2 >> c >> m2 >> cost;
            int t1 = h1 * 60 + m1;
            int t2 = h2 * 60 + m2;
            if(t1 >= 8 * 60 && t2 <= 18 * 60){
                map<string, int>::iterator it;
                int j1, j2;
                it = index.find(s1);
                if(it == index.end()){
                    j1 = n;
                    index[s1] = j1;
                    ++ n;
                    edges.resize(n);
                    revEdges.resize(n);
                }else{
                    j1 = it->second;
                }
                it = index.find(s2);
                if(it == index.end()){
                    j2 = n;
                    index[s2] = j2;
                    ++ n;
                    edges.resize(n);
                    revEdges.resize(n);
                }else{
                    j2 = it->second;
                }
                t1 -= 8 * 60;
                t2 -= 8 * 60;
                edges[j1].push_back(Edge(j2, t1, t2, cost));
                revEdges[j2].push_back(Edge(j1, t1, t2, cost));
            }
        }

        vector<vector<vector<int> > > minCost(4);
        solve1(0, minCost[0]);
        solve1(1, minCost[1]);
        solve2(0, minCost[2]);
        solve2(1, minCost[3]);
        int ret = INF;
        for(int i=0; i<n; ++i){
            for(int j=0; j<600; ++j){
                minCost[0][i][j+1] = min(minCost[0][i][j+1], minCost[0][i][j]);
                minCost[1][i][j+1] = min(minCost[1][i][j+1], minCost[1][i][j]);
            }
            for(int j=600; j>0; --j){
                minCost[2][i][j-1] = min(minCost[2][i][j-1], minCost[2][i][j]);
                minCost[3][i][j-1] = min(minCost[3][i][j-1], minCost[3][i][j]);
            }
            for(int j=0; j<=570; ++j)
                ret = min(ret, minCost[0][i][j] + minCost[1][i][j] + minCost[2][i][j+30] + minCost[3][i][j+30]);
        }

        if(ret < INF)
            cout << ret << endl;
        else
            cout << 0 << endl;
    }
}