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
#include<iostream>
#include<queue>
#include<string>
#include<map>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<set>
#include<cstdio>
#include<cassert>

using namespace std;

#define foreach(i,o) for(__typeof((o).begin()) i = (o).begin(); i != (o).end(); ++i)

const int max_place = 101;
const int max_time = 601;
const int MAX_V = 2010;
const int inf = 1<<28;

struct edge{
  int src, dst;
  int cost;
  edge(int src, int dst, int cost):src(src), dst(dst), cost(cost){}
};

struct state{
  int pos;
  int prev;
  int cost;
  
  state(int pos, int prev, int cost):pos(pos),prev(prev),cost(cost){}

  bool operator>(const state &t)const{
    if(cost!=t.cost)return cost>t.cost;
    if(prev!=t.prev)return prev>t.prev;
    if(pos!=t.pos)return pos>t.pos;
    return false;
  }
  
  /*
  bool operator<(const state &t)const{
    if(pos!=t.pos)return pos<t.pos;
    return false;
  }
  */
};

typedef vector<edge> edges;
typedef vector<edges> graph;

int convTime(const string &s){
  int h = atoi(s.substr(0,2).c_str());
  int m = atoi(s.substr(3,2).c_str());
  return 60 * h + m;
}

string revTime(int t){
  char buf[32];
  sprintf(buf, "%02d:%02d", t/60, t%60);
  return string(buf);
}

static int dist[MAX_V][MAX_V];
void johnson(const graph &tmp_g){
  graph g = tmp_g;
  
  const int v = g.size();
  static int h[MAX_V];
  static int prev[MAX_V][MAX_V];
  

  for(int i = 0; i < v; ++i){
    h[i] = 0;
  }

  for(int i = 0; i <= v; ++i){
    bool end = true;
    for(int j = 0; j < v; ++j){
      for(int k = 0; k < (int)g[j].size(); ++k){
        edge e = g[j][k];
        int tmp = h[j] + e.cost;
        if( h[e.dst] > tmp ){
          h[e.dst] = tmp;
          end = false;
        }
      }
    }
    if(end)break;
    // do not detect an negative cycle.
  }

  for(int i = 0; i < v; ++i){
    for(int j = 0; j < v; ++j){
      dist[i][j] = inf;
      prev[i][j] = -1;
    }
    dist[i][i] = 0;
  }

  for(int i = 0; i < v; ++i){
    for(int j = 0; j < (int)g[i].size(); ++j){
      g[i][j].cost += h[i] - h[g[i][j].dst];
    }
  }

  for(int s = 0; s < v; ++s){
    priority_queue<state, vector<state>, greater<state> > q;
    q.push(state(s,s,0));
    while(!q.empty()){
      state cur = q.top(); q.pop();
      if(prev[s][cur.pos]!=-1)continue;
      prev[s][cur.pos] = cur.prev;
      for(int i = 0; i < (int)g[cur.pos].size(); ++i){
        edge e = g[cur.pos][i];
        int cost = cur.cost + e.cost;
        assert(cost>=0);
        if(dist[s][e.dst] > cost){
          dist[s][e.dst] = cost;
          q.push(state(e.dst,cur.pos,cost));
        }
      }
    }
    for(int i = 0; i < v; ++i){
      dist[s][i] += h[i] - h[s];
    }
  }

  return ;  
}

int main()
{
  int n;
  while(cin>>n && n){
    int id = 1;
    int vid = 0;
    map<string,int> cityDict;
    map<int,string> revCityDict;
    set<int> cityEvTimes[max_place];
    graph g(MAX_V);
    
    map<pair<int,int>, int> vID;
    map<int,pair<int,int> > revID;
    
    for(int i = 0; i < n; ++i){
      string from, ftime, to, ttime;
      int cost;

      cin >> from >> ftime >> to >> ttime >> cost;
      if(!cityDict.count(from)){
        cityDict[from] = id;
        revCityDict[id] = from;
        ++id;
      }
      if(!cityDict.count(to)){
        cityDict[to] = id;
        revCityDict[id] = to;
        ++id;
      }

      int src = cityDict[from];
      int dst = cityDict[to];
      int srcTime = convTime(ftime) - 480;
      int dstTime = convTime(ttime) - 480;
      
      if( dstTime >= 0 && dstTime < max_time && srcTime >= 0 && srcTime < max_time ){
        if( !vID.count(make_pair(src,srcTime)) ){
          vID[make_pair(src,srcTime)] = vid;
          revID[vid] = make_pair(src,srcTime);
          ++vid;
        }
        cityEvTimes[src].insert(srcTime);
        if( !vID.count(make_pair(dst,dstTime)) ){
          vID[make_pair(dst,dstTime)] = vid;
          revID[vid] = make_pair(dst,dstTime);
          ++vid;
        }
        cityEvTimes[dst].insert(dstTime);
        
        g[vID[make_pair(src,srcTime)]].push_back( edge(vID[make_pair(src,srcTime)], vID[make_pair(dst,dstTime)], cost) );
      }

    }
    g.resize(vID.size());
    
    for(int i = 1; i < id; ++i){
      // sort( it->second.begin(), it->second.end() );
      foreach(it2, cityEvTimes[i]){
        // g[it->first].push_back( edge(it->first, it->second[i], it->first, it->second[i], 0) );
        set<int>::iterator it3 = it2;
        ++it3;
        if( it3 != cityEvTimes[i].end() ){
          g[vID[make_pair(i,*it2)]].push_back( edge(vID[make_pair(i,*it2)], vID[make_pair(i,*it3)], 0) );
        }
      }
    }
    

    /*
    for(int i = 0; i < (int)g.size(); ++i){
      cout << i << ' ' << revCityDict[i] << ':' << endl;
      for(int j = 0; j < (int)g[i].size(); ++j){
        edge e = g[i][j];
        cout << revCityDict[e.src] << ' ' << revTime(e.srcTime) << ' ' << revCityDict[e.dst] << ' ' << revTime(e.dstTime) << endl;
      }
    }
    cout << "---"<<endl;
    */

    /*
    foreach(i,revID){
      cout << revCityDict[i->second.first] << ' ' << revTime(i->second.second+480) << endl;
    }
    */

    johnson(g);

    /*
    for(int i = 0; i < (int)g.size(); ++i){
      for(int j = 0; j < (int)g.size(); ++j){
        cout << i << ' ' << j << " : " << dist[i][j]  <<endl;
      }
    }
    */

    int res = inf;
    int kenInitial = cityDict["Tokyo"];
    int keikoInitial = cityDict["Hakodate"];

    //cout << kenInitial << ' ' << keikoInitial << endl;

    int ken_st, keiko_st;
    if( cityEvTimes[kenInitial].size() > 0 && cityEvTimes[keikoInitial].size() > 0 ){
      ken_st = vID[make_pair(kenInitial, *(cityEvTimes[kenInitial].begin()))];
      keiko_st = vID[make_pair(keikoInitial, *(cityEvTimes[keikoInitial].begin()))];

      //cout <<ken_st << ' ' <<keiko_st << endl;
      
      for(int i = 1; i < id; ++i){
        foreach(ev1, cityEvTimes[i]){
          int v1 = vID[make_pair(i,*ev1)];
          foreach(ev2, cityEvTimes[i]){
            int v2 = vID[make_pair(i,*ev2)];
            int st = max(*ev1, *ev2) + 30;
            int from = -1;

            if(cityEvTimes[i].lower_bound(st)!=cityEvTimes[i].end()){
              from = vID[make_pair(i,*(cityEvTimes[i].lower_bound(st)))];
            }
            if(from<0)continue;

            //cout << v1 << ' ' << v2 << ' ' << from << endl;

            foreach(ev3, cityEvTimes[kenInitial]){
              if( *ev3 < st ) continue;
              int v3 = vID[make_pair(kenInitial,*ev3)];
              foreach(ev4, cityEvTimes[keikoInitial]){
                if( *ev4 < st ) continue;
                int v4 = vID[make_pair(keikoInitial, *ev4)];
                // cout << i << ' ' << revTime(*ev1+480) << ' ' << revTime(*ev2+480) << ' ' << revTime(*ev3+480) << ' ' << revTime(*ev4+480) << ' ' << revTime(st+480) << " : " << sssp[0][i][*ev1] << ' '<<sssp[1][i][*ev2]<<' '<<sssp[2][kenInitial][*ev3]<< ' ' <<sssp[2][keikoInitial][*ev4] << endl;
                res = min(res, dist[ken_st][v1] + dist[keiko_st][v2] + dist[from][v3] + dist[from][v4]);
              }
            }
          }
        }
      }
      
    }
 

    if(res>=inf)res = 0;
    cout << res << endl;
  }
  return 0;
}