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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <bits/stdc++.h>
using namespace std;
#define INF 1010000000
#define EPS 1E-12
#define MP make_pair
#define ST first
#define ND second
#define SZ(x)((int)(x).size())
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define REP(i,n) for(int i=0; i<(n); ++i)
#define REPD(i,n) for(int i=(n)-1; i>=0; --i)
#define FOR(i,a,n) for(int i=(a); i<=(n); ++i)
#define FORD(i,a,n) for(int i=(a); i>=(n); --i)
#define FORE(i,a) for(__typeof((a).begin()) i=(a).begin();i!=(a).end();++i)
#define D(x) cerr<<#x<<" = "<<x<<", ";
#define DE(x) cerr<<#x<<" = "<<x<<endl;
#define DE2(x,y) cerr<<x<<": "<<y<<endl;
#define DE2x(x,y) cerr<<x<<": "<<#y<<" = "<<y<<endl;
#define D2(t,a,b,n,m) {FOR(_i_, a, n){FOR(_j_, b, m)cerr<</*setw(3)*/" "<<t[_i_][_j_];cerr<<endl;}}
#define B1(w) "("<<w<<")"
#define B2(w,x) "("<<w<<","<<x<<")"
#define B3(w,x,y) "("<<w<<","<<x<<","<<y<<")"
#define B4(w,x,y,z) "("<<w<<","<<x<<","<<y<<","<<z<<")"

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int UI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

template<class c1>
ostream& _out(ostream& ost, c1 a, c1 b);

template<class c1, class c2>
ostream &operator <<(ostream &ost, pair<c1, c2> _var);
template<class c1>
ostream &operator <<(ostream &ost, vector<c1> _var)
	{return _out(ost, ALL(_var));}
template<class c1>
ostream &operator <<(ostream &ost, deque<c1> _var)
	{return _out(ost, ALL(_var));}
template<class c1>
ostream &operator <<(ostream &ost, queue<c1> _var)
	{return _out(ost, ALL(_var));}
template<class c1>
ostream &operator <<(ostream &ost, list<c1> _var)
	{return _out(ost, ALL(_var));}
template<class c1, class c2>
ostream &operator <<(ostream &ost, set<c1, c2> _var)
	{return _out(ost, ALL(_var));}
template<class c1, class c2, class c3>
ostream &operator <<(ostream &ost, map<c1, c2, c3> _var)
	{return _out(ost, ALL(_var));}
template<class c1, class c2>
ostream &operator <<(ostream &ost, multiset<c1, c2> _var)
	{return _out(ost, ALL(_var));}
template<class c1, class c2, class c3>
ostream &operator <<(ostream &ost, multimap<c1, c2, c3> _var)
	{return _out(ost, ALL(_var));}

template<class c1, class c2>
ostream &operator <<(ostream &ost, pair<c1, c2> _var)
	{return ost<<"("<<_var.ST<<","<<_var.ND<<")";}
template<class c1>
ostream& _out(ostream& ost, c1 a, c1 b) 
	{ost<<"{";if(a!=b){ost<<*a;while(++a!=b)ost<<" "<<*a;}return ost<<"}";}

int v[501][1001];
int il[501];
vector<int> rz(1, 1);
int wz[1001];
int pr[1002];
int n, m;

bool kmp()
{
	int k = -1;
	REP(i, SZ(rz))
	{
		while(k >= 0 && wz[k + 1] != rz[i])
			k = pr[k];
		
		if(wz[k + 1] == rz[i])
			k++;
		if(k == m - 1)
			return true;
	}
	return false;
}

int main()
{
	scanf("%d%d", &n, &m);
	
	FOR(i, 1, n)
	{
		scanf("%d", &il[i]);
		
		REP(j, il[i])
			scanf("%d", &v[i][j]);
	}
	
	REP(i, m)
		scanf("%d", &wz[i]);
		
	pr[0] = pr[m] = -1;
	int k = -1;
	
	FOR(i, 1, m - 1)
	{
		k = pr[i - 1];
		
		while(k >= 0 && wz[k + 1] != wz[i])
			k = pr[k];
		
		if(wz[k + 1] == wz[i])
			k++;
		pr[i] = k;
		// DE2x(i, pr[i])
	}
	
	FOR(i, 1, 1000)
	{
		if(kmp())
		{
			printf("%d\n", i);
			return 0;
		}
		
		vector<int> nw;
		
		REP(j, SZ(rz))
			REP(z, il[rz[j]])
				nw.PB(v[rz[j]][z]);
		
		swap(nw, rz);
		// DE(rz)
		// cin>>k;
		// DE(SZ(rz))
		if(SZ(rz) > 10000000)
		{
			printf("%d\n", -1);
			return 0;
		}
	}
	
	return 0;
}
/*
*/