#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <map>
using namespace std;
int c;
string dest[26];
int dist[26][26];
void add(string s){
	bool is = true;
	for(int i=0; i<c; i++){
		if(s == dest[i]){
			is = false;
			break;
		}
	}
	if(is){
		dest[c] = s;
		c++;
	}
}
void in(string d1, string d2, int n){
	int n1, n2;
	for(int i=0; i<c; i++){
		if(d1 == dest[i])
			n1 = i+1;
		if(d2 == dest[i])
			n2 = i+1;
	}
	dist[n1][n2] = n;
}
int main(){
	ifstream fin("DATA5.txt");
	ofstream fout("OUT5.txt");
	int N, p;
	string d1, d2;
	for(int T=0; T<5; T++){
		for(int i=0; i<26; i++)
			for(int j=0; j<26; j++)
				dist[i][j] = 999999;
		//memset(dist, 0x7F, sizeof dist);
		fin>>N;
		for(int i=0; i<N; i++){
			fin>>d1>>d2>>p;
			add(d1);
			add(d2);
			in(d1,d2,p);
		}
		int a1=0, a2=0;
		for(int i=0; i<c; i++){
			if(dest[i] == "YYZ")
				a1 = i+1;
			if(dest[i] == "SEA")
				a2 = i+1;
		}
		for(int i=0; i<=25; i++)
			for(int j=0; j<=25; j++)
				for(int k=0; k<=25; k++)
					dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);

		fout<<dist[a1][a2]<<endl;
	}
	return 0;
}

