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<string>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
char in[20];
vector<pair<pair<int,int>,int> >g[110][1440];
vector<pair<pair<int,int>,int> >g2[110][1440];
int ijk[110][1440];
int v[110][1440];
int s[4][110][1440];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<110;i++)for(int j=0;j<1440;j++){
			g[i][j].clear();
			g2[i][j].clear();
		}
		map<string,int>m;
		m["Hakodate"]=0;
		m["Tokyo"]=1;
		int n=2;
		for(int i=0;i<a;i++){
			scanf("%s",in);
			string na=in;
			if(!m.count(na))m[na]=n++;
			int p=m[na];
			int H,M;
			scanf("%d:%d",&H,&M);
			int t1=H*60+M;
			scanf("%s",in);
			string nb=in;
			if(!m.count(nb))m[nb]=n++;
			int q=m[nb];
			scanf("%d:%d",&H,&M);
			int t2=H*60+M;
			int r;
			scanf("%d",&r);
			g[p][t1].push_back(make_pair(make_pair(q,t2),r));
			g2[q][t2].push_back(make_pair(make_pair(p,t1),r));
		}
		for(int i=0;i<110;i++)for(int j=0;j<1440;j++){
			v[i][j]=0;ijk[i][j]=99999999;
		}
		priority_queue<pair<int,pair<int,int> > > Q;
		Q.push(make_pair(0,make_pair(0,480)));
		ijk[0][480]=0;
		while(Q.size()){
			int cost=-Q.top().first;
			int at=Q.top().second.first;
			int t=Q.top().second.second;
			Q.pop();
			if(v[at][t])continue;
			v[at][t]=1;
			if(t<1080&&!v[at][t+1]&&ijk[at][t+1]>cost){
				ijk[at][t+1]=cost;
				Q.push(make_pair(-cost,make_pair(at,t+1)));
			}
			for(int i=0;i<g[at][t].size();i++){
				int x=g[at][t][i].first.first;
				int y=g[at][t][i].first.second;
				int z=g[at][t][i].second;
				if(!v[x][y]&&ijk[x][y]>cost+z){
					ijk[x][y]=cost+z;
					Q.push(make_pair(-cost-z,make_pair(x,y)));
				}
			}
		}
		for(int i=0;i<n;i++)for(int j=0;j<1440;j++)
			s[0][i][j]=ijk[i][j];
		for(int i=0;i<110;i++)for(int j=0;j<1440;j++){
			v[i][j]=0;ijk[i][j]=99999999;
		}
		Q.push(make_pair(0,make_pair(1,480)));
		ijk[1][480]=0;
		while(Q.size()){
			int cost=-Q.top().first;
			int at=Q.top().second.first;
			int t=Q.top().second.second;
			Q.pop();
			if(v[at][t])continue;
			v[at][t]=1;
			if(t<1080&&!v[at][t+1]&&ijk[at][t+1]>cost){
				ijk[at][t+1]=cost;
				Q.push(make_pair(-cost,make_pair(at,t+1)));
			}
			for(int i=0;i<g[at][t].size();i++){
				int x=g[at][t][i].first.first;
				int y=g[at][t][i].first.second;
				int z=g[at][t][i].second;
				if(!v[x][y]&&ijk[x][y]>cost+z){
					ijk[x][y]=cost+z;
					Q.push(make_pair(-cost-z,make_pair(x,y)));
				}
			}
		}
		for(int i=0;i<n;i++)for(int j=0;j<1440;j++)
			s[1][i][j]=ijk[i][j];
		for(int i=0;i<110;i++)for(int j=0;j<1440;j++){
			v[i][j]=0;ijk[i][j]=99999999;
		}
		Q.push(make_pair(0,make_pair(0,1080)));
		ijk[0][1080]=0;
		while(Q.size()){
			int cost=-Q.top().first;
			int at=Q.top().second.first;
			int t=Q.top().second.second;
			Q.pop();
			if(v[at][t])continue;
			v[at][t]=1;
			if(t>480&&!v[at][t-1]&&ijk[at][t-1]>cost){
				ijk[at][t-1]=cost;
				Q.push(make_pair(-cost,make_pair(at,t-1)));
			}
			for(int i=0;i<g2[at][t].size();i++){
				int x=g2[at][t][i].first.first;
				int y=g2[at][t][i].first.second;
				int z=g2[at][t][i].second;
				if(!v[x][y]&&ijk[x][y]>cost+z){
					ijk[x][y]=cost+z;
					Q.push(make_pair(-cost-z,make_pair(x,y)));
				}
			}
		}
		for(int i=0;i<n;i++)for(int j=0;j<1440;j++)
			s[2][i][j]=ijk[i][j];
		for(int i=0;i<110;i++)for(int j=0;j<1440;j++){
			v[i][j]=0;ijk[i][j]=99999999;
		}
		Q.push(make_pair(0,make_pair(1,1080)));
		ijk[1][1080]=0;
		while(Q.size()){
			int cost=-Q.top().first;
			int at=Q.top().second.first;
			int t=Q.top().second.second;
			Q.pop();
			if(v[at][t])continue;
			v[at][t]=1;
			if(t>480&&!v[at][t-1]&&ijk[at][t-1]>cost){
				ijk[at][t-1]=cost;
				Q.push(make_pair(-cost,make_pair(at,t-1)));
			}
			for(int i=0;i<g2[at][t].size();i++){
				int x=g2[at][t][i].first.first;
				int y=g2[at][t][i].first.second;
				int z=g2[at][t][i].second;
				if(!v[x][y]&&ijk[x][y]>cost+z){
					ijk[x][y]=cost+z;
					Q.push(make_pair(-cost-z,make_pair(x,y)));
				}
			}
		}
		for(int i=0;i<n;i++)for(int j=0;j<1440;j++)
			s[3][i][j]=ijk[i][j];
		int ret=999999999;
		for(int i=0;i<n;i++){
			for(int j=480;j<=1410;j++){
				ret=min(ret,s[0][i][j]+s[1][i][j]+s[2][i][j+30]+s[3][i][j+30]);
			}
		}
		if(ret>99999999)printf("0\n");
		else printf("%d\n",ret);
	}
}