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<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define ALL(C)     (C).begin(),(C).end()
#define pb         push_back
#define mp         make_pair

const int N = 100;
const int inf = (1 << 28);

class Edge{
public:
  int sh,sm,dh,dm;
  int d;
  int cost;
};

class state{
public:
  int now,h,m;
  int cost;
  bool operator<(const state & a)const{
    return cost > a.cost;
  }
};

bool isless(int h,int m,int nh,int nm){
  if (h < nh)return true;
  else if (h == nh && m <= nm)return true;
  return false;
}

bool isgreater(int h,int m,int nh,int nm){
  if (h > nh)return true;
  else if (h == nh && m >= nm)return true;
  return false;
}

void dijkstra(int n,int s,vector<Edge>*edge,int cost[24][60][N],
	      bool isrev){
  rep(i,24)rep(j,60)rep(k,n)cost[i][j][k]=inf;
  priority_queue<state> Q;
  if (isrev)Q.push((state){s,17,59,0});
  else Q.push((state){s,8,0,0});

  while(!Q.empty()){
    state now = Q.top();Q.pop();
    if (cost[now.h][now.m][now.now] != inf)continue;
    cost[now.h][now.m][now.now]=now.cost;
    rep(i,edge[now.now].size()){
      if (isrev){
	if (isgreater(now.h,now.m,edge[now.now][i].sh,edge[now.now][i].sm))
	  Q.push((state){edge[now.now][i].d,edge[now.now][i].dh,
			   edge[now.now][i].dm,
			   now.cost+edge[now.now][i].cost});
      }else {
	if (isless(now.h,now.m,edge[now.now][i].sh,edge[now.now][i].sm))
	  Q.push((state){edge[now.now][i].d,edge[now.now][i].dh,
			   edge[now.now][i].dm,
			   now.cost+edge[now.now][i].cost});
      }
    }
  }


  if (isrev){
    rep(k,100){
      int tmp = inf;
      for(int i=17;i>=6;i--){
	for(int j=59;j>=0;j--){
	  cost[i][j][k]=min(cost[i][j][k],tmp);
	  tmp=min(cost[i][j][k],tmp);
	}
      }
    }
  }else {
    rep(k,100){
      int tmp = inf;
      rep(i,24){
	rep(j,60){
	  cost[i][j][k]=min(cost[i][j][k],tmp);
	  tmp=min(tmp,cost[i][j][k]);
	}
      }
    }
  }
}

//from tokyo,from hakodate, to tokyo,to hakodate
int ft[24][60][N],fh[24][60][N],tt[24][60][N],th[24][60][N];
int solve(int n,int hakodate,int tokyo,vector<Edge>*edge,
	  vector<Edge>*revedge){


  dijkstra(n,tokyo,edge,ft,false);
  dijkstra(n,hakodate,edge,fh,false);
  dijkstra(n,tokyo,revedge,tt,true);
  dijkstra(n,hakodate,revedge,th,true);

  
  int ret = inf;
  rep(k,n){
    int tmp = inf;
    REP(i,8,18){
      rep(j,30){
	tmp=min(tmp,ft[i][j][k]+fh[i][j][k]+tt[i][j+30][k]+th[i][j+30][k]);
      }
      REP(j,30,60){
	tmp=min(tmp,ft[i][j][k]+fh[i][j][k]+tt[i+1][(j+30)%60][k]+th[i+1][(j+30)%60][k]);
      }
    }
    ret=min(ret,tmp);
  }
  return ret;
}


int getnum(string &a,map<string,int> & M){
  int index=M.size();
  if (M.find(a) == M.end())M[a]=index;
  return M[a];
}

int main(){
  int n;
  vector<Edge> edge[N];
  vector<Edge> rev[N];
  const string name[2]={"Hakodate","Tokyo"};
  while(cin>>n && n){
    rep(i,N)edge[i].clear(),rev[i].clear();
    map<string,int> M;

    string from,to;
    int f,t;
    int sh,sm,dh,dm;
    int cost;
    char dummy;
    rep(i,n){
      cin>>from>>sh>>dummy>>sm;
      cin>>to>>dh>>dummy>>dm;
      cin>>cost;
      f=getnum(from,M);
      t=getnum(to,M);
      edge[f].pb((Edge){sh,sm,dh,dm,t,cost});
      rev[t].pb((Edge){dh,dm,sh,sm,f,cost});
    }

    
    int ans = solve(M.size(),M[name[0]],M[name[1]],edge,rev);
    if (ans == inf)cout <<0<<endl;
    else cout << ans << endl;

  }
}