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 <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

const int INF = (1<<28);
const int STR = (8*60);
const int LIM = (18*60);

class Train
{
public:
	int src,dst,dep,arv,cst;
	Train(int src, int dst, int dep, int arv, int cst)
		:src(src),dst(dst),dep(dep),arv(arv),cst(cst)
	{}
};

class State
{
public:
	int p,tm,cst,pvT, pvC;
	State(int p, int tm, int cst, int pvT, int pvC)
		:p(p),tm(tm),cst(cst),pvT(pvT),pvC(pvC)
	{}

	bool operator<(const State& s) const
	{
		if(cst != s.cst) return cst > s.cst;
		return tm > s.tm;
	}
};

typedef vector<Train> City;
typedef vector<vector<int> > Matrix;

int toMin(string s)
{
	stringstream ss(s);
	int h,m;
	char t;
	ss >> h >> t >> m;

	return h*60+m;
}

bool used[100][2005];

int dijkstra_go(int S, int T, int u, vector<City>& cities)
{
	priority_queue<State> q;

	for(int i=0; i<cities[S].size(); i++)
		q.push(State(cities[S][i].dst, cities[S][i].arv, cities[S][i].cst, i, S));

	memset(used, 0, sizeof(used));

	while(!q.empty()) {
		State s=q.top(); q.pop();
		if(used[s.pvC][s.pvT]) continue;
		used[s.pvC][s.pvT] = true;

		if(s.pvC == T && s.pvT == u) return s.cst-cities[s.pvC][s.pvT].cst;

		for(int i=0; i<cities[s.p].size(); i++) {
			Train t = cities[s.p][i];
			if(used[s.p][i]) continue;
			if(s.tm > t.dep) continue;
			if(i == u && s.p == T && s.tm + 30 > t.dep) continue;

			q.push(State(t.dst, t.arv, s.cst+t.cst, i, s.p));
		}
	}

	return INF;
}

int dijkstra_back(int S, int T, int u, vector<City>& cities)
{
	if(S == T) return 0;

	priority_queue<State> q;
	q.push(State(cities[S][u].dst, cities[S][u].arv, cities[S][u].cst, u, S));

	memset(used, 0, sizeof(used));

	while(!q.empty()) {
		State s = q.top(); q.pop();
		if(used[s.pvC][s.pvT]) continue;
		used[s.pvC][s.pvT] = true;

		if(s.p == T) return s.cst;

		for(int i=0; i<cities[s.p].size(); i++) {
			Train t = cities[s.p][i];
			if(used[s.p][i]) continue;
			if(s.tm > t.dep) continue;

			q.push(State(t.dst, t.arv, s.cst+t.cst, i, s.p));
		}
	}

	return INF;
}

int solve(int Hakodate, int Tokyo, vector<City>& cities)
{
	Matrix goKen(cities.size());
	for(int i=0; i<cities.size(); i++) {
		goKen[i].resize(cities[i].size());
		for(int j=0; j<cities[i].size(); j++) {
			goKen[i][j] = INF;
		}
	}

	Matrix backKen(goKen), goKeiko(goKen), backKeiko(goKen);

	for(int i=0; i<cities.size(); i++)
	for(int j=0; j<cities[i].size(); j++) {
		goKen[i][j] = dijkstra_go(Hakodate, i, j, cities);
		goKeiko[i][j] = dijkstra_go(Tokyo, i, j, cities);
		backKen[i][j] = dijkstra_back(i, Hakodate, j, cities);
		backKeiko[i][j] = dijkstra_back(i, Tokyo, j, cities);
	}

	int res = INF;
	for(int i=0; i<cities.size(); i++)
	for(int j=0; j<cities[i].size(); j++)
	for(int k=0; k<cities[i].size(); k++)
	{
		int g = (cities[i][j].dep < cities[i][k].dep ? j : k);
		res = min(res, goKeiko[i][g]+goKen[i][g]+backKeiko[i][j]+backKen[i][k]);
	}

	if(res == INF) res = 0;

	return res;
}

int main()
{
	int N;
	while(cin >> N, N) {
		
		int cnt=0;
		map<string, int> dic;
		vector<City> cities(N);
		for(int i=0; i<N; i++) {
			string src,dst,t,s;
			int c;
			cin >> src >> t >> dst >> s >> c;
			
			if(!dic.count(src)) dic[src] = cnt++;
			if(!dic.count(dst)) dic[dst] = cnt++;

			int l = toMin(t), r = toMin(s);
			if(l < STR || r < STR|| l > LIM || r > LIM) continue;

			cities[dic[src]].push_back(Train(dic[src], dic[dst], l, r, c));
		}

		int hako = dic["Hakodate"];
		int tokyo = dic["Tokyo"];

		cities[hako].push_back(Train(hako, hako, LIM, LIM, 0));
		cities[tokyo].push_back(Train(tokyo, tokyo, LIM, LIM, 0));

		cout << solve(hako, tokyo, cities) << endl;
	}

}