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<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100
#define BASE 480
#define MAX 600

enum Type{
	A,
	B,
};

struct Edge{
	Edge(int arg_to,int arg_cost,int arg_to_time){
		to = arg_to;
		cost = arg_cost;
		to_time = arg_to_time;
	}
	int to,cost,to_time;
};

int N;
int node_index;
int min_cost[2][NUM][601],return_min_cost[2][NUM][601];
int A_id = 0,B_id = 1;
const string start_A = "Hakodate";
const string start_B = "Tokyo";

map<string,int> MAP;
map<int,int> CALC;
vector<Edge> G[NUM][601],rev_G[NUM][601];

//時刻を分に直す
int getNUM(char work[6]){

	int hour = 10*(work[0]-'0')+(work[1]-'0');
	int minute = 10*(work[3]-'0')+(work[4]-'0');

	return 60*hour+minute;
}

//8時～18時の時間帯か
bool rangeCheck(int tmp_time){
	if(tmp_time >= BASE && tmp_time <= BASE+MAX)return true;
	else{
		return false;
	}
}

void func(){

	MAP.clear();
	for(int i = 0; i < NUM; i++){
		for(int k = 0; k <= MAX; k++){
			G[i][k].clear();
			rev_G[i][k].clear();
		}
	}

	string start,arrival;
	char start_buf[6],arrive_buf[6];
	int start_time,arrive_time;
	int price,start_id,arrive_id;

	MAP[start_A] = A_id;
	MAP[start_B] = B_id;

	node_index = 2;

	for(int loop = 0; loop < N; loop++){
		cin >> start >> start_buf >> arrival >> arrive_buf >> price;

		auto at = MAP.find(start);

		if(at == MAP.end()){
			MAP[start] = node_index;
			start_id = node_index;
			node_index++;

		}else{
			start_id = MAP[start];
		}

		at = MAP.find(arrival);

		if(at == MAP.end()){
			MAP[arrival] = node_index;
			arrive_id = node_index;
			node_index++;

		}else{
			arrive_id = MAP[arrival];
		}

		start_time = getNUM(start_buf);
		arrive_time = getNUM(arrive_buf);

		if(rangeCheck(start_time) == false || rangeCheck(arrive_time) == false)continue;

		start_time -= BASE;
		arrive_time -= BASE;

		G[start_id][start_time].push_back(Edge(arrive_id,price,arrive_time));
		rev_G[arrive_id][arrive_time].push_back(Edge(start_id,price,start_time));
	}

	for(int i = 0; i < 2; i++){
		for(int town = 0; town < node_index; town++){
			for(int time = 0; time <= MAX; time++){
				min_cost[i][town][time] = BIG_NUM;
				return_min_cost[i][town][time] = BIG_NUM;
			}
		}
	}

	min_cost[A][0][0] = 0;
	min_cost[B][1][0] = 0;

	for(int i = 0; i < 2; i++){
		for(int time = 0; time < MAX; time++){
			for(int town = 0; town < node_index; town++){
				min_cost[i][town][time+1] = min(min_cost[i][town][time+1],min_cost[i][town][time]);
				for(int a = 0; a < G[town][time].size(); a++){
					min_cost[i][G[town][time][a].to][G[town][time][a].to_time] = min(min_cost[i][G[town][time][a].to][G[town][time][a].to_time],
							min_cost[i][town][time]+G[town][time][a].cost);
				}
			}
		}
	}

	return_min_cost[A][0][MAX] = 0;
	return_min_cost[B][1][MAX] = 0;

	for(int i = 0; i < 2; i++) {
		for(int time = MAX; time > 0; time--) {
			for(int town = 0; town < node_index; town++) {
				return_min_cost[i][town][time-1] = min(return_min_cost[i][town][time-1],return_min_cost[i][town][time]);
				for(int a = 0; a < rev_G[town][time].size(); a++) {
					return_min_cost[i][rev_G[town][time][a].to][rev_G[town][time][a].to_time] =
							min(return_min_cost[i][rev_G[town][time][a].to][rev_G[town][time][a].to_time],
												return_min_cost[i][town][time]+rev_G[town][time][a].cost);
				}
			}
		}
	}

	int ans = BIG_NUM;
	for(int time = 0; time+30 <= MAX; time++) {
		for(int town = 0; town < node_index; town++) {
			if(min_cost[A][town][time] == BIG_NUM || min_cost[B][town][time] == BIG_NUM ||
					return_min_cost[A][town][time+30] == BIG_NUM || return_min_cost[B][town][time+30] == BIG_NUM)continue;
			ans = min(ans, min_cost[A][town][time]+min_cost[B][town][time]+return_min_cost[A][town][time+30]+return_min_cost[B][town][time+30]);
		}
	}

	if(ans == BIG_NUM){
		printf("0\n");
	}else{
		printf("%d\n",ans);
	}
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}
	return 0;
}