#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 vs vector<string>

//#define DEBUG

using namespace std;

int gr[108][108];
int sn;
int dist[108];

int dfs(int node,int depth){
	dist[node] = depth;
	for(int i=1; i<=100; ++i){
		if(gr[node][i]){
			if(dist[i]!=-1) return depth-dist[i]+1;
			else{
				int k = dfs(i,depth+1);
				if(k) return k;
			}
		}
	}
	return -1;
}

int main(){
	
	#ifndef DEBUG
		freopen("DATA5.txt", "r", stdin);
		freopen("OUT5.txt", "w", stdout);
	#endif
	
	forr(_r,0,5){
		int n;
		cin >> n;
		memset(gr,0,sizeof(gr));
		memset(dist,-1,sizeof(dist));
		
		sn=-1;
		forr(i,0,n){
			int a, b;
			cin >> a >> b;
			gr[a][b]=1;
			if(sn==-1) sn=a;
		}
		
		cout << dfs(sn,0) << '\n';
		
	}
	
	#ifdef DEBUG
		system("pause");
	#endif
	
	return 0;
}

