import java.io.*;
import java.util.Arrays;
import java.util.Scanner;


public class TravelPlans {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// File to read
		Scanner sc = new Scanner(new FileReader("data5.txt"));
		PrintWriter pw = new PrintWriter(new FileWriter("out5.txt"));
		
		for (int k=0; k<5; k++)
		{
			String cities[] = new String[20];
			int dist[][] = new int[20][20];
			int numCities = 0;
			
			int n = sc.nextInt();
			for (int i=0; i<n; i++)
			{
				String city1 = sc.next();
				String city2 = sc.next();
				int d = sc.nextInt();
				
				if (i==0)
				{
					cities[0] = city1;
					cities[1] = city2;
					dist[0][1] = d;
					numCities = 2;
				} else {
					// Look for existing entry
					int found1 = findCity(city1, cities, numCities);
					int found2 = findCity(city2, cities, numCities);
					
					if (found1 == -1) 
					{
						cities[numCities] = city1;
						found1 = numCities;
						numCities++;
					}
					if (found2 == -1) 
					{
						cities[numCities] = city2;
						found2 = numCities;
						numCities++;
					}
					dist[found1][found2] = d;
			}
			}
		int bestPath = findBestPath("YYZ", "SEA", cities, numCities, dist);
		pw.println(bestPath);
	}
	pw.close();
}

	private static int findBestPath(String city1, String city2,
			String[] cities, int numCities, int[][] dist) {
		int best = -1; // no path
		int found1 = findCity(city1, cities, numCities);
		int found2 = findCity(city2, cities, numCities);
		
		// Direct flight
		if (dist[found1][found2] > 0)
		{
			best = dist[found1][found2];
		}
		
		for (int i=0; i<numCities; i++)
		{
			if ( (dist[found1][i] > 0) && (i != found2) )
			{
				// found connection
				int restofTrip = findBestPath(cities[i], city2, cities, numCities, dist);
				if ( (restofTrip != -1) && ( (dist[found1][i]+restofTrip) < best) )
				{
					best = dist[found1][i]+restofTrip;
				}
			}
		}
		
		return best;
	}

	private static int findCity(String city1, String cities[], int numCities) {
		int result=-1;
		int j=0;
		while ( (result == -1) && (j < numCities) )
		{
			if (cities[j].equals(city1))
			{
				result = j;
			}
			j++;
		}
		return result;
	}
}
