#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin("DATA5.txt");
    ofstream fout("OUT5.txt");
    int map[101],map2[101],times,a,b,temp;
    for (int count=0;count<5;count++){
    for (int i=0;i<101;i++){
        map[i]=0;
        map2[i]=0;
    }
    fin>>times;
    for (int i=0;i<times;i++){
        fin>>a>>b;
        if (i==0){
            map2[a]=1;
            temp=a;
        }
        map[a]=b;
    }
    a=temp;
    for (int k=1;;k++){
        map2[map[a]]++;
        if (map2[map[a]]>1){
            fout<<k<<endl;
            break;
        }
        a=map[a];
    }
    }
    return 0;
}

