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 <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <iterator>
#include <bitset>
#include <random>
#include <assert.h>
#include <unordered_map>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;

const int N = 110;
const int T = 610;

int cost[4][N*T];

inline int f(int n, int t){
	return n*T + t;
}

vector<P> G[N*T], rev[N*T];
vector<int> v[N];

void dijk(int start, int t, int *opt, vector<P> *graph){
	opt[f(start, t)] = 0;
	priority_queue<P, vector<P>, greater<P>> q;
	q.push(MP(0, f(start, t)));

	while (!q.empty()){
		int d = q.top().first;
		int pos = q.top().second;

		q.pop();

		if (opt[pos] < d) continue;

		for (auto &e : graph[pos]){
			if (opt[e.first] <= d + e.second)continue;
			opt[e.first] = d + e.second;
			q.push(MP(d + e.second, e.first));
		}
	}
}

void pp(int i, int pos, int t){
	cout << (i % 2 ? "to" : "from") << " " << (i / 2 ? "Hakodate" : "Tokyo") << endl;
	cout << cost[i][f(pos, t)] << endl;
}

int main(){
	int n;
	while (cin >> n, n){
		rep(i, N*T) G[i].clear();
		rep(i, N*T) rev[i].clear();
		rep(i, N) v[i].clear(), v[i].push_back(0), v[i].push_back(600);

		map<string, int> m;
		int idx = 0;
		m["Tokyo"] = idx++;
		m["Hakodate"] = idx++;
		rep(i, n){
			string s, t;
			int h1, m1, h2, m2, cost;
			char c;
			cin >> s >> h1 >> c >> m1 >> t >> h2 >> c >> m2 >> cost;
			int t1 = h1 * 60 + m1 - 480, t2 = h2 * 60 + m2 - 480;
			if (0 <= t1 && t1 <= 600 && 0 <= t2 && t2 <= 600){
				if (!m.count(s)) m[s] = idx++;
				if (!m.count(t)) m[t] = idx++;
				int from = f(m[s], t1), to = f(m[t], t2);
				G[from].push_back(MP(to, cost));
				rev[to].push_back(MP(from, cost));
				v[m[s]].push_back(t1);
				v[m[t]].push_back(t2);
			}
		}

		rep(i, idx){
			sort(ALL(v[i]));
			v[i].erase(unique(ALL(v[i])), v[i].end());
			rep(j, v[i].size()){
				if (j + 1 < v[i].size()) G[f(i, v[i][j])].push_back(MP(f(i, v[i][j + 1]), 0));
				if (j - 1 >= 0) rev[f(i, v[i][j])].push_back(MP(f(i, v[i][j - 1]), 0));
			}
		}

		rep(i, 4){
			MEMSET(cost[i], 0x7f);
			// Tokyo -> x, x -> Tokyo, Hakodate -> x, x -> Hakodate
			dijk(i / 2, i % 2 ? 600 : 0, cost[i], i % 2 ? rev : G);
		}

		rep(k, 2) rep(i, idx){
			for (int j = 0+1; j <= 600; ++j){
				cost[k * 2][f(i, j)] = min(cost[k * 2][f(i, j)], cost[k * 2][f(i, j - 1)]);
			}
			for (int j = 600-1; j >= 0; --j){
				cost[k * 2 + 1][f(i, j)] = min(cost[k * 2 + 1][f(i, j)], cost[k * 2 + 1][f(i, j + 1)]);
			}
		}

		int ans = 1e9;
		rep(i, idx) for (int j = 0; j <= 600 - 30; ++j){
			//pp(0, i, j);
			//pp(1, i, j+30);
			//pp(2, i, j);
			//pp(3, i, j+30);
			int a = f(i, j), b = f(i, j + 30);
			if ((cost[0][a] | cost[1][b] | cost[2][a] | cost[3][b]) >= ans) continue;
			ans = min(ans, cost[0][a] + cost[1][b] + cost[2][a] + cost[3][b]);
		}
		cout << (ans < 1e9 ? ans : 0) << endl;
	}



	return 0;
}