import java.io.*;
import java.util.*;

public class Q5 {
	static Node[] nodes = new Node[100];
	static int numLinks = 0;
	public static void main(String[] args) {
		try{
			//open read/write
			Scanner fin = new Scanner(new File("DATA5.txt"));
			PrintStream fout = new PrintStream(new FileOutputStream(new File("OUT5.txt")));
			
			//local variables
			int[] inInt = new int[5];
			
			int[] outInt = new int[5];
			
			for(int z = 0; z<10; z++) {
				nodes[z] = new Node(z);
			}
			
			//read DATA1.txt
			for(int i = 0; i < 5; i++) {
				int numNodes = 0;
				for(int z = 0; z<100; z++) {
					nodes[z] = new Node(z);
				}
				inInt[i] = Integer.parseInt(fin.nextLine());
				
				for(int j = 0; j < inInt[i]; j++) {
				
					String tStr = fin.nextLine();
					String tempStr = tStr;
					System.out.println(tempStr);
					
					tempStr = tempStr.substring(tempStr.indexOf(" ".charAt(0))+1);
					System.out.println("Linking node " + tStr.substring(0,tStr.indexOf(" ".charAt(0))) + " with " + tempStr + ".");
					nodes[Integer.parseInt(tStr.substring(0,tStr.indexOf(" ".charAt(0))))].addLink(Integer.parseInt(tempStr));
					numNodes = j;
				}
				for(int j =0; j < numNodes+1; j++) {
					if(findLink(j+1, nodes[j+1], 0) == false) {
						System.out.println("Did not find matches");
					} else {
						outInt[i] = numLinks+1;
						System.out.println(numLinks+1);
						break;
					}
				}
			}
			
			
			//write OUT1.txt
			for(int i = 0; i < 5; i++) {
				fout.println(outInt[i]);
			}
			
			//close read/write
			fin.close();
			fout.close();
		} catch(IOException e) {
		}
	}
	
	public static boolean findLink(int tLink, Node node, int Links) {
		if(node.getId() != 0) {
			System.out.println("Finding links for node " + node.getId() + ". It has " + node.getNumLinks() + " links.");
			int[] links = new int[node.getNumLinks()];
			links = node.getLinks();
			for(int i = 0; i < node.getNumLinks()+1; i++) {
				System.out.println("Matching " + links[i] + " with " + tLink);
				if(links[i]==tLink) {
					numLinks = Links;
					System.out.println("Matched link");
					return true;
				} else {
					if(nodes[links[i]].getId() != 0) {
						System.out.println("Calling findLink for " + nodes[links[i]].getId());
						if(findLink(tLink, nodes[links[i]], Links+1) == true) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}
}

class Node {
	int id = 0;
	int links = 0;
	int[] link = new int[100];
	
	public Node(int ids) {
		id = ids;
	}
	
	public void addLink(int linkss) {
		link[links] = linkss;
		links++;
	}
	
	public int getId() {
		return id;
	}
	
	public int[] getLinks() {
		return link;
	}
	
	public int getNumLinks() {
		return links;
	}
}
