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<map>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=((1<<31)-1)/4;

struct train{ int u,v,t1,t2,cost; };
bool cmp0(const train &T1,const train &T2){ return T1.t1<T2.t1 || T1.t1==T2.t1 && T1.t2<T2.t2; } // (t1,t2) の昇順
bool cmp1(const train &T1,const train &T2){ return T1.t2>T2.t2 || T1.t2==T2.t2 && T1.t1>T2.t1; } // (t2,t1) の降順

int decode(const char *t){
	int hh=(t[0]-'0')*10+(t[1]-'0');
	int mm=(t[3]-'0')*10+(t[4]-'0');
	return 60*hh+mm-8*60;
}

void calc_time(int n,int m,train *T,int dp[2][601][2001],int s){
	rep(i,2) rep(t,601) rep(u,n) dp[i][t][u]=INF;

	sort(T,T+m,cmp0);
	dp[0][0][s]=0;
	int iter=0;
	rep(t,601){
		if(t<600){ // 動かずに一分過ごす
			rep(u,n) dp[0][t+1][u]=min(dp[0][t+1][u],dp[0][t][u]);
		}
		while(iter<m && T[iter].t1==t){ // 電車に乗る
			dp[0][T[iter].t2][T[iter].v]=min(dp[0][T[iter].t2][T[iter].v],dp[0][t][T[iter].u]+T[iter].cost);
			iter++;
		}
	}

	sort(T,T+m,cmp1);
	dp[1][600][s]=0;
	iter=0;
	for(int t=600;t>=0;t--){
		if(t>0){
			rep(u,n) dp[1][t-1][u]=min(dp[1][t-1][u],dp[1][t][u]);
		}
		while(iter<m && T[iter].t2==t){
			dp[1][T[iter].t1][T[iter].u]=min(dp[1][T[iter].t1][T[iter].u],dp[1][t][T[iter].v]+T[iter].cost);
			iter++;
		}
	}
}

int main(){
	for(int m;scanf("%d",&m),m;){
		int n=0;
		map<string,int> f;
		f["Hakodate"]=n++;
		f["Tokyo"]=n++;

		train T[2000];
		rep(i,m){
			char s1[32],t1[8],s2[32],t2[8];
			int cost; scanf("%s%s%s%s%d",s1,t1,s2,t2,&cost);
			if(f.count(s1)==0) f[s1]=n++;
			if(f.count(s2)==0) f[s2]=n++;
			int tt1=decode(t1),tt2=decode(t2);
			if(0<=tt1 && tt2<=600){
				T[i]=(train){f[s1],f[s2],tt1,tt2,cost};
			}
			else{
				i--;
				m--;
			}
		}

		// 頂点が多すぎて MLE するので, 函館と東京の両方からたどり着ける頂点だけに制限する
		vector<int> G[4000];
		rep(i,m) G[T[i].u].push_back(T[i].v);
		bool vis[2][4000]={};
		rep(i,2){
			vis[i][i]=true;
			int Q[4000],head=0,tail=0;
			Q[tail++]=i;
			while(head<tail){
				int u=Q[head++];
				rep(j,G[u].size()){
					int v=G[u][j];
					if(!vis[i][v]) vis[i][v]=true, Q[tail++]=v;
				}
			}
		}
		bool kill[4000]={};
		vector<int> V;
		rep(u,n){
			if(!vis[0][u] || !vis[1][u]) kill[u]=true;
			else                         V.push_back(u);
		}
		rep(i,m){
			if(kill[T[i].u] || kill[T[i].v]){
				swap(T[i],T[m-1]);
				i--;
				m--;
			}
			else{
				T[i].u=lower_bound(V.begin(),V.end(),T[i].u)-V.begin();
				T[i].v=lower_bound(V.begin(),V.end(),T[i].v)-V.begin();
			}
		}

		// dp1[0][t][u] := ( 8:00 に函館にいて, 時刻 t に駅 u にいるときの最小コスト )
		// dp1[1][t][u] := ( 18:00 に函館に戻れて, 時刻 t に駅 u にいるときの最小コスト )
		// dp2 は dp1 の函館を東京に変えただけ.
		static int dp1[2][601][2001],dp2[2][601][2001];
		calc_time(n,m,T,dp1,0);
		calc_time(n,m,T,dp2,1);

		int ans=INF;
		rep(u,n) rep(t,600-30+1) {
			ans=min(ans,dp1[0][t][u]+dp1[1][t+30][u]+dp2[0][t][u]+dp2[1][t+30][u]);
		}
		printf("%d\n",ans<INF?ans:0);
	}

	return 0;
}