import java.io.*;
import java.util.*;

public class Q5 {
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(new File("DATA5.txt"));
		PrintStream o = new PrintStream(new File("OUT5.txt"));
		HashMap <String,Port> ports;
		int edgecount;
		String source;
		String dest;
		int cost;
		for(int z = 0; z < 5; z++) {
			edgecount = in.nextInt();
			ports = new HashMap<String,Port>();
			for(int i = 0; i < edgecount; i++) {
				source = in.next();
				dest = in.next();
				cost = in.nextInt();
				if(!ports.containsKey(source)) {
					ports.put(source,new Port(source));
				}
				if(!ports.containsKey(dest)) {
					ports.put(dest,new Port(dest));
				}
				ports.get(source).addPath(ports.get(dest), cost);
			}
			int min = 999999;
			ArrayList<String> queue = new ArrayList<String>();
			ArrayList<String> dist = new ArrayList<String>();
			ArrayList<ArrayList<String>> checked = new ArrayList<ArrayList<String>>();
			queue.add("YYZ");
			dist.add("0");
			checked.add(new ArrayList<String>());
			while(!queue.isEmpty()) {
				String pos = queue.get(0);
				int trav = Integer.parseInt(dist.get(0));
				ArrayList<String> chec = (ArrayList<String>)checked.get(0).clone();
				chec.add(pos);
				String[] aPorts = ports.get(pos).getPorts();
				if(pos.equals("SEA")) {
					if(trav < min) {
						min = trav;
					}
				} else {
					for(int i = 0; i < aPorts.length; i++) {
						if(!chec.contains(aPorts[i])) {
							checked.add(chec);
							Port newPort = ports.get(pos);
							int newDist = newPort.getCost(ports.get(aPorts[i]));
							dist.add(Integer.toString(trav + newDist));
							queue.add(aPorts[i]);
						}
					}
				}
				queue.remove(0);
				checked.remove(0);
				dist.remove(0);
			}
			o.println(min);
		}
		in.close();
		o.close();
		System.exit(0);
	}	
}

class Port {
	String name;
	HashMap<Port,String> paths;
	public Port(String n) {
		name = n;
		paths = new HashMap<Port,String>();
	}
	
	void addPath(Port p, int c) {
		paths.put(p,Integer.toString(c));
	}
	
	int getCost(Port p) { 
		return Integer.parseInt(paths.get(p));
	}
	
	String[] getPorts() {
		Port[] keys = paths.keySet().toArray(new Port[0]);
		String[] out = new String[keys.length];
		for(int i = 0; i < out.length; i++) {
			out[i] = keys[i].getName();
		}
		return out;
	}
	
	String getName() {
		return name;
	}
	
}
