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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include<bits/stdc++.h>
#define FOR(i,s,e) for(int i=(s);i<=(e);i++)
#define FORD(i,s,e) for(int i=(s);i>=(e);i--)
#define ALL(k) (k).begin(),(k).end()
#define e1 first 
#define e2 second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<int,PII> PIP;
typedef pair<PLL,PLL> PPP;
const LL TYS=1000;
const LL MLD=TYS*TYS*TYS;

const int MAXN=1111;
const LL MOD=MLD+696969;
const LL BASE=2131;

vector<int> trans[MAXN];
int slowo[MAXN];


LL dlu[30][MAXN];
int prefs[3][MAXN][MAXN];
int inside[3][MAXN][MAXN];
int sufs[3][MAXN][MAXN];
int win[30][MAXN];
LL hasz[30][MAXN];
LL h1[MAXN];
LL potb[MAXN];
LL hh(int a,int b){
	if(a==b) return 0;
	LL ans=(h1[b]-(h1[a]*potb[b-a])%MOD)%MOD;
	if(ans<0) ans+=MOD;
	return ans;
}
//vector<int> v69[MAXN][MAXN];
int tpr[MAXN][50];
int tsf[MAXN][50];
int vic[MAXN][MAXN];
int vis[MAXN][MAXN];


vector<PII> V1[MAXN*MAXN];
main(){
	int n,m;scanf("%d%d",&n,&m);
	FOR(i,1,n){
		int l;scanf("%d",&l);
		while(l--){
			int a;scanf("%d",&a);
			trans[i].PB(a);
		}
		hasz[0][i]=i;
	}
	potb[0]=1;
	FOR(i,1,m) {
		scanf("%d",&slowo[i]);
		h1[i]=(h1[i-1]*BASE+slowo[i])%MOD;
		potb[i]=(potb[i-1]*BASE)%MOD;
	}
	h1[m+1]=(h1[m]*BASE)%MOD;
	potb[m+1]=(potb[m]*BASE)%MOD;
	FOR(i,1,n){
		dlu[0][i]=1;
		if(slowo[1]==i) prefs[0][i][1]=1;
		if(slowo[m]==i) sufs[0][i][m]=1;
		if(m==1&&slowo[1]==i) win[0][i]=1;
	}
	FOR(i,1,m) inside[0][slowo[i]][i]=1;
	//FOR(i,1,n) v69[0][i].PB(i);
	FOR(k,1,22){
		int k1=k%2,k2=(k1+1)%2;
		FOR(i,1,n)
			FOR(j,1,m)
				prefs[k1][i][j]=sufs[k1][i][j]=inside[k1][i][j]=0;
		FOR(i,1,n){
			//aktualizacja prefiksow
			LL obh=0;
			LL obdl=0;
			int vs=trans[i].size()-1;
			//printf("PREFIX %d\n",i);
			FORD(j,vs,0){
				int pos=trans[i][j];
				if(obdl>m) break;
				//printf("%d %d %lld %lld\n",j,pos,obh,obdl);
				FOR(l,0,m){
					if(obdl+l>m) break;
					if(prefs[k2][pos][l]==0) continue;
					//printf("\t%d %lld %lld\n",l,obh,hh(l,obdl+l));
					if(obh==hh(l,obdl+l)) prefs[k1][i][obdl+l]=1;
				}
				if(obdl+dlu[k-1][pos]>m) break;
				obh+=(hasz[k-1][pos]*potb[obdl])%MOD;
				obh%=MOD;
				obdl+=dlu[k-1][pos];
			}
			//puts("A");
			//sufiksow
			obh=0;
			obdl=0;
			FOR(j,0,vs){
				int pos=trans[i][j];
				if(obdl>=m) break;
				FORD(l,m,0){
					if(l<=obdl) break;
					if(sufs[k2][pos][l]==0) continue;
					if(obh==hh(l-obdl-1,l-1)) sufs[k1][i][l-obdl]=1;
				}
				if(obdl+dlu[k-1][pos]>m) break;
				obh=(obh*potb[dlu[k-1][pos]]+hasz[k-1][pos])%MOD;
				obdl+=dlu[k-1][pos];
			}
			//puts("B");
			//calosci
			FOR(l,1,m){
				int ob=l;
				if(inside[k2][trans[i][0]][ob]==0) continue;
				int ok=1;
				ob+=dlu[k-1][trans[i][0]];
				FOR(j,1,vs){
					if(ob>m) {ok=0;break;}
					if(inside[k2][trans[i][j]][ob]==0) {ok=0;break;}
					ob+=dlu[k-1][trans[i][j]];
				}
				if(ok) inside[k1][i][l]=1;
			}
			//puts("C");
			//szukam czy mam juz w srodku ciong
			FOR(j,0,vs){
				FOR(l,1,m){
					if(prefs[k2][trans[i][j]][l]==0) continue;
					int ob=l+1;
					FOR(j1,j+1,vs){
						if(sufs[k2][trans[i][j1]][ob]){
							win[k][i]=1;
							break;
						}
						if(inside[k2][trans[i][j1]][ob]==0) break;
						ob+=dlu[k-1][trans[i][j1]];
					}
				}
			}		
			//puts("D");
			FOR(j,0,vs){
				dlu[k][i]+=dlu[k-1][trans[i][j]];
				if(win[k-1][trans[i][j]]) win[k][i]=1;
				if(dlu[k-1][trans[i][j]]<=m)
					hasz[k][i]=(hasz[k][i]*potb[dlu[k-1][trans[i][j]]]+hasz[k-1][trans[i][j]])%MOD;
			}
		}
		if(win[k][1]) {printf("%d\n",k+1);return 0;}
	}
	//puts("A");
	/*FOR(k,0,5){
		FOR(i,1,n){
			if(k>0){
				for(auto it:v69[k-1][i])
					for(auto b:trans[it])
						v69[k][i].PB(b);
			}
		}
		printf("k==%d::\n",k);
		FOR(i,1,n){
			printf("%d:: ",i);
			for(auto it:v69[k][i]) printf("%d ",it);
			puts("");
		}
		FOR(i,1,n){
			printf("%d %d %lld %d\n",k,i,dlu[k][i],win[k][i]);
			printf("prefs: ");
			FOR(j,0,m) if(prefs[k1][i][j]) printf("%d ",j);
			puts("");
			printf("suf: ");
			FOR(j,0,m) if(sufs[k1][i][j]) printf("%d ",j);
			puts("");
			printf("inside: ");
			FOR(j,0,m) if(inside[k1][i][j]) printf("%d ",j);
			puts("");
		}
	}*/
	FOR(i,1,n){
		int j=0;
		int j1=0;
		while(j<=m){
			FOR(k,0,29){
				tpr[i][j1]*=2;
				tpr[i][j1]+=prefs[0][i][j+k];
				tsf[i][j1]*=2;
				tsf[i][j1]+=sufs[0][i][j+k+1];
			}
			j1++;
			j+=30;
		}
	}
	//puts("B");
	FOR(i,1,n){
		FOR(j,1,n){
			FOR(k,0,40) 
				if(win[22][i]||win[22][j]||(tpr[i][k]&tsf[j][k])>0) 
					{vic[i][j]=1;break;}
			//if(vic[i][j]) printf("%d %d\n",i,j);
		}
	}
	
	//puts("C");
	FORD(i,trans[1].size()-1,1) V1[23].EB(trans[1][i-1],trans[1][i]);
	//puts("D");
	for(int k=23;true;k++){
		//if(V1[k].empty()){printf("ZLE %d\n",k); break;}
		//if(k>62000) printf("%d:: ",k);
		for(auto it:V1[k]){
			int a=it.e1,b=it.e2;
			//if(k>62000) printf("%d %d %d\n",a,b,vic[a][b]);
			if(vic[a][b]||win[22][a]||win[22][b]) {printf("%d\n",k+1);return 0;}
			vis[a][b]=1;
			a=trans[a].back();b=trans[b][0];
			if(vis[a][b]==0)V1[k+1].EB(a,b);
		}
		//if(k>62000)puts("");
	}
	puts("-1");	
	
}