1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#include<iostream>
#include<vector>


using namespace std;

int pwalizki(int n,vector <int> v[], int akt[])
{
	
	int j=0;
	int a;
	int m;
	while(j!=n-1)
	{
		
		//cout<<j+1<<" ";
		m=v[j].size()-2;
		a=akt[j];

		akt[j]+=1;
		akt[j]=akt[j]%m;
		//cout<<v[j][a]<<" ";
		j=v[j][a]-1;
		
		//cout<<j+1<<"  ";
		//cout<<v[j][a]<<endl;
			
		
		
		
		
		
	}
	
	
}

bool check(int akt[],int n)
{
	int z=0;
	bool ver=true;
	for(z=0;z<n;z++)
	{
		if(akt[z]>0)
		{
			ver=false;
			break;
		}
		z++;
	}
	return ver;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin>>n;
	vector <int> v[307];
	int akt[307]={0};
	
	for(int i=0;i<n;i++)
	{
		
		int r; cin>>r;
		if(r==0)
		{
			v[i].push_back(i+2);
			//cout<<v[i][0];
		}
		for(int j=0;j<r;j++)
		{
			int l; cin>>l;
			v[i].push_back(l);
			//cout<<v[i][j]<<endl;
			
		}
		v[i].push_back(0);
		v[i].push_back(0);
		
	}
	pwalizki(n,v,akt);
	int counter=1;
	bool verif=check(akt,n);
	while(verif==false)
	{
		pwalizki(n,v,akt);
		verif=check(akt,n);
		counter++;
		
	}
	cout<<counter;
	
	/*for(int w=0;w<akt.size();w++)
	{
		cout<<akt[w]<<" ";
	}*/
	
	
	
	
	
	
	
	
	
	
}