#include<iostream>
#include<cmath>
#include<math.h>
#include<string>
#include <fstream>
#include <cctype>
#include <iomanip>
#include <algorithm>
using namespace std;

int a2[100];
pair<int,int> a[100];
int n,x,y,z,check,c,ch;

void cycle(int q)
{
	int v,w,m;
	if(ch)
		return;
	for(v=0;v<n;v++)
	{
		if(q==a[v].first)
		{
			a2[c]=q;
			c++;
			for(w=0;w<c;w++)
				for(m=0;m<c;m++)
					if(a2[w]==a2[m] && w!=m)
					{
						ch=1;
						cout << m-w << endl;
						return;
					}
			cycle(a[v].second);
		}
	}
	c--;
	return;
}



int main()
{
	freopen("DATA5.txt","r",stdin);
	freopen("OUT5.txt","w",stdout);
	for(x=0;x<5;x++)
	{
		c=ch=0;
		check=1;
		cin >> n;
		for(y=0;y<100;y++)
			a2[y]=0;
		for(y=0;y<n;y++)
		{
			a[y].first=a[y].second=0;
		}
		for(y=0;y<n;y++)
			cin >> a[y].first >> a[y].second;
		for(y=0;y<n;y++)
			if(a[y].first==a[y].second)
			{
				cout << "1" << endl;
				check=0;
				break;
			}
		if(check)
		{
			cycle(a[0].first);
		}
	}
	return(0);
}
