// Arup Guha
// 11/20/08
// Proposed Solution to 2008 SE Regional Problem: Series/Parallel Circuits
// Note: This was written during a team "exercise" racing against the other
//       members of the team I coach while they were working on different
//       problems from the same regional.

import java.util.*;
import java.io.*;

public class a {
	
	final static double EPSILON = 0.000000001;
	
	public static void main(String[] args) throws Exception {
		
		
		Scanner fin = new Scanner(new File("a.in"));
		
		int n = fin.nextInt();
		
		while (n != 0) {
			
			// Stores adjacency matrix, stays size 26, so some rows are zeroed out.
			double[][] adj = new double[26][26];
			
			// No connections yet.
			for (int i=0; i<26; i++) {
				for (int j=0; j<26; j++)
					adj[i][j] = 0;
			}
			
			// Tells me which nodes still exist.
			int[] degree = new int[26];
			
			for (int i=0; i<26; i++) degree[i] = 0;
			
 			// Initial circuit, only take care of obvious parallel cases.
			for (int i=0; i<n; i++) {
				
				String res1 = fin.next();
				String res2 = fin.next();
				double resVal = fin.nextDouble();
				int node1 = (int)(res1.charAt(0) - 'A');
				int node2 = (int)(res2.charAt(0) - 'A');
					
				// No resistor there already.
				if (Math.abs(adj[node1][node2]) < EPSILON) {
					adj[node1][node2] = resVal;
					adj[node2][node1] = resVal;	
					degree[node1]++;
					degree[node2]++;			
				}
				
				// Parallel case, do this immediately.
				else {
					double oldRes = adj[node1][node2];
					double newRes = oldRes*resVal/(oldRes+resVal);
					adj[node1][node2] = newRes;
					adj[node2][node1] = newRes;	
				}
				
			}
			
			boolean bad = false;
			
			while (!done(degree)) {
				
				// find a node to remove
				int remNode = find(degree);
				if (remNode == -1) {
					bad = true;
					break;
				}
				remove(adj, degree, remNode);
			}
			
			if (bad)
				System.out.println("-1.000");
			else
				System.out.printf("%.3f\n",adj[0][25]);
			
			n = fin.nextInt();
		}
	}
	
	public static boolean done(int[] deg) {
		if (deg[0] != 1) return false;
		if (deg[25] != 1) return false;
		
		for (int i=1; i<25; i++)
			if (deg[i] != 0)
				return false;
				
		return true;
	}
	
	public static int find(int[] deg) {
		for (int i=1; i<25; i++)
			if (deg[i] == 2)
				return i;
		return -1;
	}
	
	public static void remove(double[][] adj, int[] degree, int remNode) {
		
		int i, saveFirst=0, saveSecond=0;
		for (i=0; i<26; i++)
			if (Math.abs(adj[remNode][i]) > EPSILON) {
				saveFirst = i;
				break;
			}
			
		double res1 = adj[remNode][saveFirst];
		
		i++;
		while (i<26) {
			if (Math.abs(adj[remNode][i]) > EPSILON) {
				saveSecond = i;
				break;
			}
			i++;
		}
		
		double res2 = adj[remNode][saveSecond];
		
		double resVal = res1+res2;
		
		// parallel case
		if (Math.abs(adj[saveFirst][saveSecond]) > EPSILON) {
			
			double oldRes = adj[saveFirst][saveSecond];
			double newRes = oldRes*resVal/(oldRes+resVal);
			adj[saveFirst][saveSecond] = newRes;
			adj[saveSecond][saveFirst] = newRes;
			degree[saveFirst]--;
			degree[saveSecond]--;
			
		}
		else {
			adj[saveFirst][saveSecond] = resVal;
			adj[saveSecond][saveFirst] = resVal;
		}
		
		// zero out two old links and degree matrix
		degree[remNode] = 0;
		adj[remNode][saveFirst] = 0;
		adj[saveFirst][remNode] = 0;
		adj[remNode][saveSecond] = 0;
		adj[saveSecond][remNode] = 0;
			
	}
}