



orig="YYZ"
dest="SEA"
locations={}
costs=[]
inFile = open("DATA5.txt")
outFile = open("OUT5.txt", "w")
#outFile.write("HI")

def add(loc, key, val):
    if key in loc:
        loc[key] = min(loc[key], val)
    else:
        loc[key] = val
    


def condense(flyTo):
    lowest = 100000000000
    for flyFrom, cost in locations[flyTo].items():
        if flyFrom == orig :
            lowest = min(lowest, cost)
        elif flyFrom in locations:            
            for i in locations[flyFrom]:
                lowest = min(lowest, condense(flyFrom)+cost)
                
    return lowest
            
        


for group in range(5):
    flights = []
    locations ={}
    iFlights = int(inFile.readline().strip())
    print(iFlights)
    for i in range(iFlights):
        flyFrom, flyTo, cost =(inFile.readline().split())
        cost = int(cost)
        if not flyTo in locations:
            locations[flyTo] = {flyFrom:cost}
        else:
            add(locations[flyTo], flyFrom, cost)
#            if flyFrom in locations[flyTo]:
#                locations[flyTo][flyFrom] = min(locations[flyTo][flyFrom], cost)
#            else:
#                locations[flyTo][flyFrom]=cost
    print(locations)
    costs.append(condense(dest))
#    print(cost)
print(costs)
for i in costs:
    outFile.write(str(i)+"\n")

        

