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

### constant

MINM =  8 * 60
MAXM = 18 * 60
MEET = 30

INF = 1 << 30

### subroutines

def hm2min(hm)
  h, m = hm.split(':').map(&:to_i)
  h * 60 + m
end

def min_costs_from(tts, ncts, nmins, from)
  mcf = ncts.times.map{nmins.times.map{INF}}
  mcf[from] = nmins.times.map{0}

  tts.each do |smid, gmid, scid, gcid, cost|
    if mcf[scid][smid] < INF
      gcost = mcf[scid][smid] + cost
      if mcf[gcid][gmid] > gcost
        for m in (gmid...nmins)
          mcf[gcid][m] = gcost if mcf[gcid][m] > gcost
        end
      end
    end
  end

  mcf
end

def min_costs_to(rtts, ncts, nmins, to)
  mct = ncts.times.map{nmins.times.map{INF}}
  mct[to] = nmins.times.map{0}

  rtts.each do |smid, gmid, scid, gcid, cost|
    if mct[gcid][gmid] < INF
      scost = mct[gcid][gmid] + cost
      if mct[scid][smid] > scost
        for m in (0..smid)
          mct[scid][m] = scost if mct[scid][m] > scost
        end
      end
    end
  end

  mct
end

### main

loop do
  n = gets.to_i
  break if n == 0

  chash = {}
  mhash = {}
  tts = []

  n.times do
    sct, shm, gct, ghm, cost = gets.strip.split

    smin = hm2min(shm) - MINM
    gmin = hm2min(ghm) - MINM
    next if smin < 0 || gmin > MAXM - MINM

    chash[sct] = chash[gct] = true
    mhash[smin] = mhash[gmin] = true

    tts << [sct, smin, gct, gmin, cost.to_i]
  end
  #p tts

  cts = chash.keys.sort
  cts.each_with_index{|c, i| chash[c] = i}
  ncts = cts.length
  #p cts

  mins = mhash.keys.sort
  mins.each_with_index{|m, i| mhash[m] = i}
  nmins = mins.length
  #p mins

  tts = tts.map do |sct, smin, gct, gmin, cost|
    [mhash[smin], mhash[gmin], chash[sct], chash[gct], cost]
  end.sort

  cid0 = chash['Hakodate']
  cid1 = chash['Tokyo']

  mcf0 = min_costs_from(tts, ncts, nmins, cid0)
  mcf1 = min_costs_from(tts, ncts, nmins, cid1)

  mcf = ncts.times.map{|cid| nmins.times.map{|mid|
      sum = mcf0[cid][mid] + mcf1[cid][mid]
      sum < INF ? sum : INF
    }}

  rtts = tts.sort{|a, b| c = b[1] <=> a[1]; (c != 0) ? c : b[0] <=> a[0]}

  mct0 = min_costs_to(rtts, ncts, nmins, cid0)
  mct1 = min_costs_to(rtts, ncts, nmins, cid1)

  mct = ncts.times.map{|cid| nmins.times.map{|mid|
      sum = mct0[cid][mid] + mct1[cid][mid]
      sum < INF ? sum : INF
    }}

  min_cost = INF

  for cid in (0...ncts)
    for fmid in (0...nmins)
      cost_f = mcf[cid][fmid]
      next if cost_f >= INF
      fmin = mins[fmid]

      for tmid in ((fmid + 1)...nmins)
        next if mins[tmid] - fmin < MEET
        cost_t = mct[cid][tmid]
        break if cost_t >= INF

        cost = cost_f + cost_t
        min_cost = cost if min_cost > cost
      end
    end
  end

  puts (min_cost < INF) ? min_cost : 0
end