import java.io.*;
import java.util.*;
public class P5
{

    static int mincost = Integer.MAX_VALUE;
    static HashMap places, airportscosts;
    public static void recurse (int cost, String from, String to)
    {
	if (from.equals (to))
	{
	    mincost = Math.min (cost, mincost);
	    return;
	}
	int ii = ((Integer) airportscosts.get (from)).intValue ();
	if (ii<cost)return;
	ArrayList t = (ArrayList) places.get (from);
	// System.out.println (from + " " + to + " " + t);
	for (int a = 0 ; a < t.size () ; a++)
	{
	    Airport air = ((Airport) t.get (a));
	    int c = air.cost;
	    // if (!visited.contains (air.name))
	    // {
	    //     visited.add (air.name);
	    airportscosts.put (from, new Integer(cost + c));



	    recurse (cost + c, air.name, to);
	    // }

	}
    }



    public static void main (String[] args) throws IOException
    {
	BufferedReader in = new BufferedReader (new FileReader ("DATA5.txt"));
	PrintWriter out = new PrintWriter (new FileWriter ("OUT5.txt"));

	for (int pp = 0 ; pp < 5 ; pp++)
	{
	    int i = Integer.parseInt (in.readLine ());

	    places = new HashMap ();
	    airportscosts = new HashMap ();
	    for (int x = 0 ; x < i ; x++)
	    {
		String s = in.readLine ();
		String from = s.split (" ") [0];
		String to = s.split (" ") [1];
		Integer cost = new Integer (s.split (" ") [2]);
		//                System.out.println (x + " " + from);

		if (places.containsKey (from))
		{
		    Airport a = new Airport ();
		    a.name = to;
		    a.cost = cost.intValue ();
		    a.been = false;
		    ((ArrayList) places.get (from)).add (a);
		}
		else
		{
		    Airport a = new Airport ();
		    a.name = to;
		    a.cost = cost.intValue ();
		    a.been = false;
		    ArrayList t = new ArrayList ();
		    t.add (a);
		    places.put (from, t);
		}
		airportscosts.put (from, new Integer (Integer.MAX_VALUE));
	    }
	    recurse (0, "YYZ", "SEA");
	    out.println (mincost);
	    mincost = Integer.MAX_VALUE;
	}
	in.close ();
	out.close ();
    }


    private static class Airport
    {
	public String name;
	public int cost;
	public boolean been;



    }
}

