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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inft = 1000000009;
const int MAXN = 2005,MAXV=1000006;
const int DEB=0,RAN=12;

int n,m;
map<pii,int> M;
vi H[MAXN],MP[MAXN][RAN],MS[MAXN][RAN],P;
vector<pii> V[MAXV];
pii KMPP[MAXN][RAN];
int pi[MAXN],dist[MAXV];

void kmp2(int ii,int j){
#define TT MS[ii][j]
	int q=0;
	for(int i=0;i<TT.size();++i){ //tekst od 0-wej pozycji
		while(q && TT[i]!=P[q+1]) q=pi[q];
		if(TT[i]==P[q+1]) ++q;
		if(q==m){
			KMPP[ii][j].y=1;
			return ;
			q=pi[q];
		}
	}
	KMPP[ii][j]=pii(q,0);
	return ;
}
bool kmp(vi &A, vi &B){
	int As=A.size(),Bs=B.size();
	int q=0;
#define T(a) (a<As?A[a]:B[a-As])
	for(int i=0;i<As+Bs;++i){ //tekst od 0-wej pozycji
		while(q && T(i)!=P[q+1]) q=pi[q];
		if(T(i)==P[q+1]) ++q;
		if(q==m){
			return 1;
			q=pi[q];
		}
	}
	return 0;
}
bool kmp3(int ii,int j, vi &B){
	int Bs=B.size();
	if(KMPP[ii][j].y)return 1;
	int q=KMPP[ii][j].x;
	for(int i=0;i<Bs;++i){ //tekst od 0-wej pozycji
		while(q && B[i]!=P[q+1]) q=pi[q];
		if(B[i]==P[q+1]) ++q;
		if(q==m){
			return 1;
			q=pi[q];
		}
	}
	return 0;
}

void morphP(vi &A){
	vi C;
	tr(it,A){
		tr(it2,H[*it]){
			C.pb(*it2);
			if(C.size()>m)break;
		}
		if(C.size()>m)break;
	}
	swap(A,C);
}
void morphS(vi &A){
	vi C;
	reverse(ALL(A));
	tr(it,A){
		for(int j=H[*it].size()-1;j>=0;j--){
			C.pb(H[*it][j]);
			if(C.size()>m)break;
		}
		if(C.size()>m)break;
	}
	swap(A,C);
	reverse(ALL(A));
}


void solve() {
	scanf("%d%d",&n,&m);
	fru(i,n){
		int k;
		scanf("%d",&k);
		H[i].resize(k);
		fru(j,k){scanf("%d",&H[i][j]);H[i][j]--;}
	}
	P.resize(m+1);
	P.pb(-1);//wzorzec musi byc przesuniety o 1
	fru(i,m){scanf("%d",&P[i+1]);P[i+1]--;}
	int q=0;
	pi[0]=pi[1]=0;
	for(int i=2;i<=m;++i) {
		while(q && P[i]!=P[q+1]) q=pi[q];
		if(P[i]==P[q+1]) ++q;
		pi[i]=q;
	}
	//create MS,MP
	fru(i,n){
		vi A;
		A.pb(i);
		fru(j,RAN){MP[i][j]=A;if(j+1!=RAN)morphP(A);}
	}
	fru(i,n){
		vi A;
		A.pb(i);
		fru(j,RAN){MS[i][j]=A;if(j+1!=RAN)morphS(A);}
		fru(j,RAN)kmp2(i,j);
	}
	//create graph
	int nr=1;
	fru(i,n)for(int j=-1;j<n;j++)M[pii(i,j)]=nr++;
	M[pii(-1,-1)]=nr++;
	//create graph
	//transitions (i,-1) ->***
	fru(i,n){
		int u=M[pii(i,-1)];
if(DEB)		printf("dla %d\n",i);
		//single transitions
		tr(it,H[i]){
		if(DEB)	printf("%d -> %d\n",*it,i);
			V[M[pii(*it,-1)]].pb(pii(u,1));
		}
		//pref/suf transitions
		fru(j,H[i].size()-1){
		if(DEB)	printf("%d,%d -> %d\n",H[i][j],H[i][j+1],i);
			V[M[pii(H[i][j],H[i][j+1])]].pb(pii(u,1));
		}
		//potential answers
		fru(j,H[i].size()-1){
			// H[i][j], H[i][j+1]
	if(DEB)		printf("j=%d\n",j);
			vi A,B;
			A.pb(H[i][j]);
			for(int jj=j+1;jj<H[i].size();jj++)B.pb(H[i][jj]);
			int ans;
			for(ans=1;ans<RAN;ans++){
				if(DEB){	puts("A");
				tr(it,A)printf("%d ",*it);puts("");
				puts("B");
				tr(it,B)printf("%d ",*it);puts("");
				}	if(kmp(A,B))break;
				morphS(A);
				morphP(B);
			}
		if(DEB)	printf("ans %d\n",ans);
			if(ans<RAN)V[M[pii(-1,-1)]].pb(pii(u,ans+1));
		}
	}
	//transitions (i,j)
	fru(i,n)fru(j,n){
		int u=M[pii(i,j)];
		pii v;
		v.x=H[i].back();
		v.y=H[j][0];
		V[M[v]].pb(pii(u,1));
		//check if have answer
		// i,j
		int ans;
		for(ans=1;ans<RAN;ans++){
			if(kmp3(i,ans-1,MP[j][ans-1]))break;
		}
		if(ans<RAN)V[M[pii(-1,-1)]].pb(pii(u,ans));
	}
	//najkr. sciezka z pii(0,-1) do pii(-1,-1)
	fru(i,nr)dist[i]=inft;
	dist[M[pii(-1,-1)]]=0;
	priority_queue<pii> Q;
	Q.push(pii(0,M[pii(-1,-1)]));
	while(!Q.empty()){
		pii u=Q.top();Q.pop();
		if(dist[u.y]<-u.x)continue;
		tr(it,V[u.y])if(-u.x+it->y<dist[it->x]){
			dist[it->x]=-u.x+it->y;
			Q.push(pii(-dist[it->x],it->x));
		}
	}
	if(dist[M[pii(0,-1)]]==inft)puts("-1\n");else
		printf("%d\n",dist[M[pii(0,-1)]]);
}

int main() {
	solve();
	return 0;
}