#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

ifstream reader("DATA5.txt");
ofstream writer("OUT5.txt");

int adjacency[100][100];
bool flag_pathfound = false;
int path[101];
int path_len, cur_node;

void check_for_cycle();

void find_cycle(){
	check_for_cycle();
	for(int a = 0; a < 100; ++a){
		if(adjacency[cur_node][a] == 1 && !flag_pathfound){
			path[path_len++] = cur_node;
			cur_node = a;
			find_cycle();
			cur_node = path[--path_len];
		}
	}
}

void check_for_cycle()
{
	for(int a = 0; a < path_len - 2; ++a){
		for(int b = a+1; b < path_len - 1; ++b){
			if(path[a] == path[b]){
				flag_pathfound = true;
				writer << b-a << endl;
				return;
			}
		}
	}
}

int main(){

	for(int p = 0; p < 5; ++p){
		
		int numpairs = 0;
		
		for(int a = 0; a < 100; ++a){
			for(int b = 0; b < 100; ++b){
				adjacency[a][b] = 0;
			}
			path[a] = 0;
		}
		path[101] = 0;
		
		path_len = 0;
		cur_node = 0;
		flag_pathfound = false;
		
		reader >> numpairs;
		
		int x, y;
		
		for(int a = 0; a < numpairs; ++a){
			reader >> x >> y;
			adjacency[x-1][y-1] = 1;
		}
		
		find_cycle();
	
	}
	
	
	return 0;
}

