Problem:
Problem F: Young, Poor and Busy

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.

Sample 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 for the Sample Input

11000
0
11090

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.*;
public class Main {
	final int INF = 1 << 24;
	HashMap<String, Integer> tonum;
	HashMap<Integer, Integer> timetable;
	ArrayList<D> toT, fromT, toH,fromH;
	ArrayList<ArrayList<C>> pass;
	int Tnum, Hnum, citysize, timesize;

	class C{
		int from, to, begin, end, fare;
		public C(String [] input) {
			this.from = tonum.get(input[0]);
			this.begin = calcTime(Integer.parseInt(input[1]), Integer.parseInt(input[2]));
			this.to = tonum.get(input[3]);
			this.end = calcTime(Integer.parseInt(input[4]), Integer.parseInt(input[5]));
			this.fare = Integer.parseInt(input[6]);
		}

		public String toString() {
			return "C [from=" + from + ", to=" + to + ", begin=" + begin
			           + ", end=" + end + ", fare=" + fare + "]";
		}
	}

	class D implements Comparable<D>{
		int nowpoint, nowtime, nowfare, starttime;

		public D(int nowpoint, int nowtime, int nowfare, int starttime) {
			this.nowpoint = nowpoint;
			this.nowtime = nowtime;
			this.nowfare = nowfare;
			this.starttime = starttime;
		}

		public int compareTo(D o) {
			if(this.nowtime < o.nowtime) return -1;
			if(this.nowtime > o.nowtime) return 1;
			return 0;
		}

		@Override
		public String toString() {
			return "D [nowpoint=" + nowpoint + ", nowtime=" + nowtime
			           + ", nowfare=" + nowfare + ", starttime=" + starttime + "]";
		}
	}

	private void doit(){
		Scanner sc = new Scanner(System.in);
		while(true){
			int n = sc.nextInt();
			if(n == 0) break;
			sc.nextLine();
			tonum = new HashMap<String, Integer>();
			timetable = new HashMap<Integer, Integer>();
			C [] inputdata = new C[n];
			pass = new ArrayList<ArrayList<C>>();
			for(int i = 0; i < n; i++){
				String [] inputline = sc.nextLine().split("[ :]");
				if(! tonum.containsKey(inputline[0])){
					tonum.put(inputline[0], tonum.size());
				}
				if(! tonum.containsKey(inputline[3])){
					tonum.put(inputline[3], tonum.size());
				}
				int h1 = Integer.parseInt(inputline[1]);
				int m1 = Integer.parseInt(inputline[2]);
				int h2 = Integer.parseInt(inputline[4]);
				int m2 = Integer.parseInt(inputline[5]);
				int time1 = calcTime(h1, m1);
				int time2 = calcTime(h2, m2);
				if(! timetable.containsKey(time1)){
					timetable.put(time1, timetable.size());
				}
				if(! timetable.containsKey(time2)){
					timetable.put(time2, timetable.size());
				}
				inputdata[i] = new C(inputline);
			}

//			//debug
//			for(int key: timetable.keySet()){
//				System.out.println(key + " " + timetable.get(key) );
//			}

			citysize = tonum.size();
			timesize = timetable.size();
			for(int i = 0; i < citysize; i++){
				pass.add(new ArrayList<C>());
			}
			for(int i = 0; i < n; i++){
				if(inputdata[i].begin < calcTime(8, 0) || inputdata[i].end > calcTime(18, 0)){
					continue;
				}
				pass.get(inputdata[i].from).add(inputdata[i]);
			}
			Tnum = tonum.get("Tokyo");
			Hnum = tonum.get("Hakodate");

			int ans = INF;
			for(int i = 0; i < citysize; i++){
				toT = new ArrayList<D>();
				fromT = new ArrayList<D>();
				toH = new ArrayList<D>();
				fromH = new ArrayList<D>();

				if(i == Tnum){
					fromT.add(new D(Tnum, calcTime(8, 0), 0, calcTime(18, 0)));
					toT.add(new D(Tnum, calcTime(8, 0), 0, calcTime(18, 0)));
				}
				else if(i == Hnum){
					fromH.add(new D(Hnum, calcTime(8, 0), 0, calcTime(18, 0)));
					toH.add(new D(Hnum, calcTime(8, 0), 0, calcTime(18, 0)));
				}

				//from tokyo
				calcTermination(Tnum,i, 0);
				//to tokyo
				calcTermination(i, Tnum, 1);
				//from hako
				calcTermination(Hnum,i, 0);
				//to hako
				calcTermination(i, Hnum, 1);
				//calc fare
				for(int fromTInd = 0; fromTInd < fromT.size(); fromTInd++){
					for(int toTInd = 0; toTInd < toT.size(); toTInd++){
						for(int fromHInd = 0; fromHInd < fromH.size(); fromHInd++){
							for(int toHInd = 0; toHInd < toH.size(); toHInd++){
								int lastArrive = Math.max(fromT.get(fromTInd).nowtime, fromH.get(fromHInd).nowtime);
								int fastleave = Math.min(toT.get(toTInd).starttime, toH.get(toHInd).starttime);
								int fare = fromT.get(fromTInd).nowfare + fromH.get(fromHInd).nowfare + toT.get(toTInd).nowfare + toH.get(toHInd).nowfare;
								if(fastleave - lastArrive >= 30){
									ans = Math.min(ans, fare);

								}
							}
						}
					}
				}
			}
			System.out.println(ans == INF ? 0 : ans);

		}
	}

	private void calcTermination(int from, int termination, int cd) {
		int endtime = calcTime(18, 0);
		PriorityQueue<D> open = new PriorityQueue<D>();
		if(cd == 0){
			open.add(new D(from,-1, 0, -1));
		}
		else{
			open.add(new D(from,endtime -1, 0, -1));
		}


		int [][] close = new int[citysize][timesize];
		for(int i = 0; i < citysize; i++){
			Arrays.fill(close[i], INF);
		}
		for(int i = 0; i < timesize; i++){
			close[from][i] = 0;
		}

		while(! open.isEmpty()){
			D now = open.poll();
			for(int i = 0; i < pass.get(now.nowpoint).size(); i++){
				C toobj = pass.get(now.nowpoint).get(i);
				boolean cond1;
				if(cd == 0){
					cond1 = (now.nowtime <= toobj.begin);
				}
				else{
					cond1 = ((endtime - now.nowtime) <= toobj.begin);
				}
				if(cond1){
					int nextpoint = toobj.to;
					int nexttime = toobj.end;
					int nextfare = now.nowfare + toobj.fare;
					int nextstarttime = now.starttime;
					if(nextstarttime == -1){
						nextstarttime = toobj.begin;
					}
					D next;
					if(cd == 0){
						int nexttimeInd = timetable.get(nexttime);
						next = new D(nextpoint, nexttime, nextfare, nextstarttime);
						if(close[nextpoint][nexttimeInd] <= nextfare) continue;
						for(int j = nexttimeInd; j < timesize; j++){
							close[nextpoint][j] = Math.min(close[nextpoint][j], nextfare);
						}
					}
					else{
						int nextstarttimeInd = timetable.get(nextstarttime);
						next = new D(nextpoint, endtime - nexttime, nextfare, nextstarttime);
						if(close[nextpoint][nextstarttimeInd] <= nextfare) continue;
						for(int j = nextstarttimeInd; j >= 0; j--){
							close[nextpoint][j] = Math.min(close[nextpoint][j], nextfare);
						}
					}

					if(nextpoint == termination){
						if(cd == 0){
							if(from == Tnum) fromT.add(next);
							else fromH.add(next);
						}
						else{
							next.nowtime = endtime - next.nowtime;
							if(termination == Tnum) toT.add(next);
							else toH.add(next);
						}
						continue;
					}
					open.add(next);
				}
			}
		}
	}

	private int calcTime(int h, int m){
		return h * 60 + m;
	}

	public static void main(String[] args) {
		Main obj = new Main();
		obj.doit();
	}
}