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
#include <iostream>
#include <vector>

using namespace std;

int n, m, nsmall, tmp, ile;

vector< vector<int> > v; // reguly
vector< int > e; // warunek koncowy

vector< int > previous;
vector< int > current;

int main(){
	ios_base::sync_with_stdio(0);
	current.push_back(1);
	cin>>n>>m;
	v.resize(n);
	for(int i=0; i<n; i++){
		cin>>nsmall;
		for (int j = 0; j < nsmall; ++j)
		{
			cin>>tmp;
			v[i].push_back(tmp);
		}
	}
	for (int i = 0; i < m; ++i)
	{
		cin>>tmp;
		e.push_back(tmp);
	}
	while(1){
	previous=current;
	current.clear();
	for (int i = 0; i < previous.size(); ++i)
	{
		current.insert(current.end(),	v[previous[i]-1].begin(),v[previous[i]-1].end());
	}
	
	//cout<<ile<<endl;
	ile++;
	bool OK = false;
	for (int i = 0; i < current.size(); ++i)
	{
		bool ok =true;
		for (int j = 0; ok && j < e.size(); ++j)
		{
			if(current[i+j]!=e[j]){
				ok = false;
			}
			else
			if (j+1 == e.size())
			{
				cout<<ile+1;
				return 0;
			}
		}
	}
	}


	return 0;
}