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 C (judge verdict: Accepted)

// AOJ 1229 Young, Poor and Busy
// 2018.2.20 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define HASHSIZ 499
typedef struct { char *s; char id; } HASH;
HASH hash[HASHSIZ+2], *hashend = hash + HASHSIZ;			// Hash table

int insert(char *s, char id)
{
	char *p;
	unsigned long long i = 0;
	HASH *tp;

	p = s; while (*p) i = (i<<5) + *p++;
	tp = hash + (int)(i % HASHSIZ);

	while (tp->s != NULL) {
		if (!strcmp(tp->s, s)) return tp->id;
		if (++tp == hashend) tp = hash;
	}
	tp->s = s, tp->id = id;
	return -1;
}

#define QMAX 500
typedef struct { int t; char s; int tm; } QUE;
QUE que[QMAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i)
{
	int l, r, min;
	QUE qt;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		min_heapify(min);
	}
}

void deq()
{
	que[0] = que[--qsize];
	min_heapify(0);
}

int qmax;
void enq(char s, int tm, int t)
{
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].s = s, que[i].t = t, que[i].tm = tm;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		i = min;
	}
}

#define INF 0x10101010
#define MAX 1082
#define ST  480			// 8am
#define ET  1080		// 6pm

typedef struct { char s, a; short st, at, f; } T;
T tbl[2002]; int m;
char city[101][18];
int ken[101][MAX], keko[101][MAX];
int mk[101][MAX];
char buf[60], *p;

int in()
{
	int n = 0;
	while (isdigit(*p)) n = (n<<3) + (n<<1) + (*p++ & 0xf);
	p++;
	return n;
}

int cmp(T *a, T *b) { return a->at - b->at; }

int dijkstra(int start, int goal, int tm)
{
	int i, j, s, d;
	T *tp;

	memset(mk, INF, sizeof(mk));
	mk[start][tm] = 0;
	qsize = 0;
	enq(start, tm, 0);
	while (qsize) {
		s = que[0].s, tm = que[0].tm, deq();
		if (s == goal) return mk[goal][tm];
		i = m; for (tp = tbl; i--; tp++) {
			if (tp->s != s || tp->st < tm) continue;
			d = mk[s][tm] + tp->f;
			if (mk[tp->a][tp->at] > d) {
				mk[tp->a][tp->at] = d, enq(tp->a, tp->at, d);
				for (j = tp->at; j <= ET; j++) {
					if (mk[tp->a][j] > d) mk[tp->a][j] = d;
				}
			}
		}
	}
	return INF;
}

int main()
{
	int n, i, j, u, v, ans;
	char *q;
	T *tp;

	strcpy(city[0], "Hakodate");
	strcpy(city[1], "Tokyo");

	while (fgets(p=buf, 10, stdin) && *p != '0') {
		m = in();
		memset(hash, 0, sizeof(hash));
		insert(city[0], 0);
		insert(city[1], 1);
		n = 2;

		j = 0, i = m, tp = tbl; while (i--) {
			fgets(p=buf, 60, stdin);
			q = p; while (*p > ' ') p++; *p++ = 0, strcpy(city[n], q);
			u = insert(city[n], n); if (u < 0) u = n++;
			tp->s = u, tp->st = in()*60+in();
			if (tp->st < ST) continue;

			q = p; while (*p > ' ') p++; *p++ = 0, strcpy(city[n], q);
			v = insert(city[n], n); if (v < 0) v = n++;
			tp->a = v, tp->at = in()*60+in();
			if (tp->at > ET) continue;
			tp->f = in();
			tp++, j++;
		}
		qsort(tbl, m=j, sizeof(T), cmp);

		memset(ken, INF, sizeof(ken)), memset(keko, INF, sizeof(keko));
		for (i = ST; i <= ET; i++) ken[0][i] = 0, keko[1][i] = 0;

		i = m; for (tp = tbl; i--; tp++) {
			u = ken [tp->s][tp->st] + tp->f; if (ken [tp->a][tp->at] > u) ken [tp->a][tp->at] = u;
			v = keko[tp->s][tp->st] + tp->f; if (keko[tp->a][tp->at] > v) keko[tp->a][tp->at] = v;
			u = ken [tp->a][tp->at], v = keko[tp->a][tp->at];
			for (j = tp->at; j <= ET; j++) {
				if (ken [tp->a][j] > u) ken [tp->a][j] = u;
				if (keko[tp->a][j] > v) keko[tp->a][j] = v;
			}
		}

		ans = INF;
		i = m; for (tp = tbl; i--; tp++) {
			if (tp->at + 30 > ET) break;
			u = ken[tp->a][tp->at] + keko[tp->a][tp->at];
			if (u < ans) u += dijkstra(tp->a, 0, tp->at + 30);
			if (u < ans) u += dijkstra(tp->a, 1, tp->at + 30);
			if (u < ans) ans = u;
		}
		if (ans == INF) ans = 0;
		printf("%d\n", ans);
	}
    return 0;
}