import java.util.*;
import java.io.*;
public class Solution5 {
	public static int out;
	public static int grid [][];
	public static int been [];
	public static void main (String args[]) throws Exception{
		BufferedReader r = new BufferedReader (new FileReader ("DATA5.txt"));
		PrintWriter w = new PrintWriter(new FileWriter("OUT5.txt"));
		for(int z = 0; z < 5; z++){
			int num = Integer.parseInt(r.readLine());

			if (num ==1){
				Scanner s = new Scanner(r.readLine());
				s.next();
				s.next();
				w.println (s.nextInt());
			}else{
				grid = new int [num*2][num*2];
				for (int i = 0; i<num*2; i++){
					Arrays.fill(grid [i], -1);
				}
				String word []= new String [num*2];
				int ct =2;
				word [0]="YYZ";
				word [1]="SEA";
				out = 99999999;
				for (int i =0; i<num; i++){
					Scanner s = new Scanner (r.readLine());
					String temp = s.next();
					String temp2 = s.next();
					int numtemp = s.nextInt();
					boolean work = true;
					int c,l;
					c=0;
					l=0;
					for (int j =0; j<ct; j++){
						if (temp.equals( word[j])){
							work = false;
							c =j;
							
							break;
						}
					}
					if (work){
						ct++;
						c=ct-1;
						word [c]= temp;
						//System.out.println (temp2+" "+word[c]+" "+c);
					}
					work = true;
					for (int j =0; j<ct; j++){
						if (temp2.equals( word[j])){
							work = false;
							l =j;
							//System.out.println (temp2+" "+word[j]+" "+j);
							break;
						}
					}
					if (work){
						ct++;
						l=ct-1;
						word [l]= temp2;
					}
					grid [c][l]=numtemp;
					//grid [l][c]=numtemp;		
				}
				been = new int [ct];
				recurse (0, 0,ct, 0);
				w.println (out);
			}

		}
		w.close();
	}
	public static void recurse (int current, int total, int ct, int c){
		been [c]= current;
		c ++;
		if (current == 1){
			out = Math.min(out, total);
			return;
		}
		for (int i =0; i<ct; i++){
			if (i!=current){
				boolean leave = false;
				for (int j= 0; j<c; j++){
					if (i==been[j]){
						leave = true;
						break;
					}
				}
				if (!leave){
					
					if (grid [current][i]>0){
						//System.out.println (total+" "+grid [current][i]+" "+i+" "+current);
						recurse (i, total+grid [current][i], ct, c);
						
					}		
				}
			}
		}
		been [c]=-1;
		c --;
		
	}
}

