#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <ctime>

#define forr(a,b,c) for(int (a) = (b); (a) < (c); ++ (a))
#define fore(a,b,c) for(int (a) = (b); (a) <= (c); ++ (a))

#define pii pair<int,int>
#define int3 pair<pair<int,int>, int>
#define vi vector<int>
#define ull long long
#define vs vector<string>

//#define DEBUG

using namespace std;

int main(){
	
	#ifndef DEBUG
		freopen("DATA5.txt", "r", stdin);
		freopen("OUT5.txt", "w", stdout);
	#endif
	
	forr(_r,0,5){
		int g[100][100];
		forr(i,0,100) forr(j,0,100) g[i][j] = 99999999;
		
		map<string,int> h;
		h["YYZ"] = 1;
		h["SEA"] = 2;
		
		int pos=3;
		
		int n; cin>>n;
		
		forr(i,0,n){
			string a,b;
			cin >> a >> b;
			
			if(!h[a]){
				h[a]=pos++;
			}
			if(!h[b]){
				h[b]=pos++;
			}
			
			cin >> g[h[a]][h[b]];
		}
		
		forr(k,1,pos){
			forr(i,1,pos){
				forr(j,1,pos){
					g[i][j] = min(g[i][j],g[i][k]+g[k][j]);
				}
			}
		}
		
		cout << g[1][2] << '\n';
		
	}
	
	#ifdef DEBUG
		system("pause");
	#endif
	
	return 0;
}

