#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;

  }
}

Measured memory_bytes: 0