import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;


public class Question5 {

	public static HashMap<String, Node> nodes;
	public static int maxCost = 0xFFFFFFF;

	public static void main(String[] args) throws Exception {
		BufferedWriter fileout = new BufferedWriter(new FileWriter("OUT5.txt"));
		Scanner scan = new Scanner(new FileReader("DATA5.txt"));
		while (scan.hasNextLine()) {
			nodes = new HashMap<String, Node>();
			int connections = scan.nextInt();
			for (int i = 0; i < connections; i++) {
				String start = scan.next();
				String end = scan.next();
				int cost = scan.nextInt();
				Node theNode = nodes.get(start);
				if (theNode == null) {
					nodes.put(start, new Node(start, end, cost));
				} else {
					theNode.connects.add(end);
					theNode.costs.add(cost);
				}
			}
			fileout.write(String.valueOf(recurse("YYZ", 0)));
			fileout.newLine();
		}
		fileout.close();
	}

	public static int recurse(String start, int cost) {
		if (start.equals("SEA")) return cost;

		Node theNode = nodes.get(start);
		if (theNode != null) {
			int result = maxCost;
			for (int i = 0; i < theNode.connects.size(); i++) {
				int temp = recurse(theNode.connects.get(i), cost + theNode.costs.get(i));
				if (temp < result) result = temp;
			}
			return result;
		} else return maxCost;
	}

	public static class Node {
		String name;
		ArrayList<String> connects;
		ArrayList<Integer> costs;
		boolean used = false;

		public Node(String name, String connect, int cost) {
			this.name = name;
			this.connects = new ArrayList<String>();
			this.costs = new ArrayList<Integer>();
			connects.add(connect);
			costs.add(cost);
		}
	}

}

