import java.io.*;
import java.util.*;

class Vertex implements Comparable<Vertex> {

    public final String name;
    public ArrayList<Edge> adjacencies = new ArrayList<Edge>();
    public double minDistance = Double.POSITIVE_INFINITY; // some huge number
    public Vertex previous;

    public Vertex(String name) {
        this.name = name;
    }

    public int compareTo(Vertex other) {
        return Double.compare(minDistance, other.minDistance);
    }
}

class Edge {

    public final Vertex target;
    public final double weight;

    public Edge(Vertex argTarget, double weight) {
        target = argTarget;
        this.weight = weight;
    }
}

public class _5 {

    static Map<String, Vertex> graph = new HashMap<String, Vertex>();

    public static void main(String[] args) throws IOException {
        Vertex v = null;
        Vertex v1 = null;

        Scanner fin = new Scanner(new FileReader("DATA5.txt"));
        PrintWriter fout = new PrintWriter(new FileWriter("OUT5.txt"));

        for (int i = 0; i < 5; i++) {
            graph.clear();
            int nPaths = fin.nextInt();
            for (int x = 0; x < nPaths; x++) {
                String sStart = fin.next();
                String sEnd = fin.next();
                int nWeight = fin.nextInt();
                v = new Vertex(sStart);
                v1 = new Vertex(sEnd);
                if(!graph.containsKey(sStart)) {
                     graph.put(sStart, v);
                } else {
                    v = graph.get(sStart);
                }

                if(!graph.containsKey(sEnd)) {
                    graph.put(sEnd, v1);
                } else {
                    v1 = graph.get(sEnd);
                }

                graph.get(sStart).adjacencies.add(new Edge(v1,nWeight));
            }

            Vertex start = graph.get("YYZ");
            computePaths(start);

            Vertex end = graph.get("SEA");
            fout.println((int)(end.minDistance));
        }


        fin.close();
        fout.close();
    }

    public static void computePaths(Vertex source) {
        source.minDistance = 0;
        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
        vertexQueue.add(source);

        while (!vertexQueue.isEmpty()) {
            Vertex u = vertexQueue.poll();

            for (Edge e : u.adjacencies) {
                Vertex v = e.target;
                double weight = e.weight;
                double distanceThroughU = u.minDistance + weight;
                if (distanceThroughU < v.minDistance) {
                    vertexQueue.remove(v);
                    v.minDistance = distanceThroughU;
                    v.previous = u;
                    vertexQueue.add(v);
                }
            }
        }
    }
}
