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 <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
using namespace std;

typedef long long ll;

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

class E{
public:
  int to;
  int ft,tt;
  int cost;

  E(){}
  E(int _to,int _ft,int _tt,int _cost){
    to = _to;
    ft = _ft;
    tt = _tt;
    cost = _cost;
  }
};

class State{
public:
  int id,tm,cost;

  State(){}
  State(int _id,int _tm,int _cost){
    id = _id;
    tm = _tm;
    cost = _cost;
  }

  bool operator<(const State &st)const{
    return cost > st.cost;
  }
};

int n,m;
int A,B;
int LEAVE,BACK;
int ac[102][2000],bc[102][2000];
int r_ac[102][2000],r_bc[102][2000];
map<string,int> name;
vector<E> t[102],rt[102];

int TimeToInt(string s){
  int h = (s[0] - '0') * 10 + (s[1] - '0');
  int m = (s[3] - '0') * 10 + (s[4] - '0');
  return h * 60 + m;
}

int NameToInt(string s){
  int tmp = name[s];
  if(tmp == 0){
    name[s] = name.size();
  }
  return name[s];
}

void calCost(int start,int closed[102][2000]){
  priority_queue<State> open;
  rep(i,102) rep(j,2000) closed[i][j] = -1;
  open.push(State(start,LEAVE,0));

  while(!open.empty()){
    State st = open.top(); open.pop();
    if(closed[st.id][st.tm] != -1) continue;
    closed[st.id][st.tm] = st.cost;

    rep(i,t[st.id].size()){
      E e = t[st.id][i];
      if(st.tm <= e.ft){
        open.push(State(e.to,e.tt,st.cost+e.cost));
      }
    }
  }
}

void calRevCost(int start,int closed[102][2000]){
  priority_queue<State> open;
  rep(i,102) rep(j,2000) closed[i][j] = -1;
  open.push(State(start,BACK,0));

  while(!open.empty()){
    State st = open.top(); open.pop();
    if(closed[st.id][st.tm] != -1) continue;
    closed[st.id][st.tm] = st.cost;

    rep(i,rt[st.id].size()){
      E e = rt[st.id][i];
      if(st.tm >= e.ft){
        open.push(State(e.to,e.tt,st.cost+e.cost));
      }
    }
  }

  REP(i,1,n){
    int minCost = INT_MAX;
    for(int j=1999;j>=0;j--){
      if(closed[i][j] != -1){
        minCost = min(minCost,closed[i][j]);
      }
      closed[i][j] = (minCost == INT_MAX ? -1 : minCost);
    }
  }
}

int main(){
  LEAVE = TimeToInt("08:00");
  BACK = TimeToInt("18:00");

  while(cin>>m,m){
    name.clear();
    rep(i,102){
      t[i].clear();
      rt[i].clear();
    }

    rep(i,m){
      string from,to,ft,tt;
      int cost;
      cin>>from>>ft>>to>>tt>>cost;

      int fromID = NameToInt(from);
      int toID = NameToInt(to);
      int ft_int = TimeToInt(ft);
      int tt_int = TimeToInt(tt);

      t[fromID].push_back(E(toID,ft_int,tt_int,cost));
      rt[toID].push_back(E(fromID,tt_int,ft_int,cost));
    }

    n = name.size() + 1;
    A = NameToInt("Hakodate");
    B = NameToInt("Tokyo");

    calCost(A,ac);
    calCost(B,bc);

    calRevCost(A,r_ac);
    calRevCost(B,r_bc);

    int ans = INT_MAX;
    REP(i,1,n){
      REP(atime,LEAVE,BACK+1){
        if(ac[i][atime] == -1) continue;
        REP(btime,LEAVE,BACK+1){
          if(bc[i][btime] == -1) continue;
          int maxTime = max(atime,btime);
          if(r_ac[i][maxTime+30] == -1 || r_bc[i][maxTime+30] == -1) break;
          ans = min(ans,ac[i][atime] + bc[i][btime] + r_ac[i][maxTime+30] + r_bc[i][maxTime+30]);
        }
      }
    }
    cout<<(ans == INT_MAX ? 0 : ans)<<endl;

  }
}