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 <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 501;
vector<int> H[MAXN];

bool contains(vector<int> &T, vector<int> &W) {
	for (size_t i = 0; i < T.size(); i++) {
		if (T.size() - i < W.size()) return false;
		int j = 0;
		while (j < W.size() && W[j] == T[i + j]) ++j;
		if (j >= W.size()) return true;
	}
	return false;
}

vector<int> transform(vector<int> &B) {
	vector<int> res;
	for (auto x : B)
		for (auto y : H[x])
			res.push_back(y);

	return res;
}

int main(int argc, char* argv[]) {
	int n, m, l, h;
	vector<int> S;

	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &l);
		while (l--) {
			scanf("%d", &h);
			H[i].push_back(h);
		}
	}

	while (m--) {
		scanf("%d", &h);
		S.push_back(h);
	}

	vector<int> B;
	B.push_back(1);
	int d = 1;
	while (B.size() < 100000) {
		if (contains(B, S)) {
			printf("%d\n", d);
			return 0;
		}
		B = transform(B);
		++d;
	}

	printf("-1\n");

	return 0;
}