public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		StringBuilder out = new StringBuilder();
		int nEdges = Integer.parseInt(in.readLine().trim());
		start = 1;
		target = 2;
		HashMap<String, Integer> mapNames = new HashMap<String, Integer>();
		mapNames.put("A", start);
		mapNames.put("Z", target);
		ArrayList<String[]> edges = new ArrayList<String[]>(nEdges);
		int idEdge = 3;
		for (int i = 0; i < nEdges; i++) {
			String[] e = in.readLine().trim().split(" ");
			if(!mapNames.containsKey(e[0]))
				mapNames.put(e[0], idEdge++);
			if(!mapNames.containsKey(e[1]))
				mapNames.put(e[1], idEdge++);
			edges.add(e);
		}
		Dinic G = new Dinic(1+mapNames.size());
		G.addEdge(start, 1, Integer.MAX_VALUE/2);
		for (String[] e : edges){
			int u = mapNames.get(e[0]);
			int v = mapNames.get(e[1]);
			int c = Integer.parseInt(e[2]);
			G.addEdge(u, v, c);
		}
		int maxFlow = G.maxFlow(start, target);
		out.append(maxFlow+"\n");
		System.out.print(out);
	}