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