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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>

const int MAX_N = 509;
const int MAX_M = 1009;
const int MAX_LOG_M = 10;
const int INF = 1000009;

int n, m;
int h[MAX_N][MAX_M];
int s[MAX_M];
int d[MAX_N][MAX_N];

// [i][k] długość i-tej po k-tej replikacji
int el[MAX_N][MAX_LOG_M];
// [i][k] możliwe pozycje i-tej w ciągu po k-tej replikacji
std::vector<int> eh[MAX_N][MAX_LOG_M];
// zawiera jako suffis, prefiks s
std::deque<int> prefix[MAX_N][MAX_LOG_M];
// zawiera jako prefiks, sufiks s
std::vector<int> suffix[MAX_N][MAX_LOG_M];
// [i][k] i-ta komórka po k-tej replikacji zawiera cały wzorzec
bool whole[MAX_N][MAX_LOG_M];

std::vector<int> kmp_tab(int* str, int len) {
  std::vector<int> tab(len+1);
  tab[0] = -1;
  tab[1] = 0;
  int cnd = 0;
  int i = 2;
  while (i <= len) {
    if (str[i-1] == str[cnd]) {
      ++cnd;
      tab[i] = cnd;
      ++i;
    } else if (cnd > 0) {
      cnd = tab[cnd];
    } else {
      tab[i] = 0;
      ++i;
    }
  }
  return tab;
}

std::vector<int> kmp_rev_tab(int* str, int len) {
  std::vector<int> tab(len+1);
  tab[0] = -1;
  tab[1] = 0;
  int cnd = 0;
  int i = 2;
  while (i <= len) {
    if (str[len-1-(i-1)] == str[len-1-cnd]) {
      ++cnd;
      tab[i] = cnd;
      ++i;
    } else if (cnd > 0) {
      cnd = tab[cnd];
    } else {
      tab[i] = 0;
      ++i;
    }
  }
  return tab;
}

void search(int* str, int len, int a, int e) {
  std::vector<int> tab = kmp_tab(str, len);
  // whole & suffix
  int i = 0;
  int pos = 0;
  while (pos < m) {
    if (pos+i >= m) {
      if (pos == 0) {
	whole[a][e] = true;
	return;
      } else {
	suffix[a][e].push_back(pos);
      }
    } else if (str[i] == s[pos+i]) {
      if (i == len) {
	if (pos > 0) {
	  eh[a][e].push_back(pos);
	} else if (len == m) {
	    whole[a][e] = true;
	    return;
	}
      } else {
	++i;
	continue;
      }
    }
    if (tab[i] > -1) {
      pos = pos + i - tab[i];
      i = tab[i];
    } else {
      i = 0;
      pos = pos+1;
    }
  }
  // prefix
  tab = kmp_rev_tab(str, len);
  i = 0;
  pos = 1;
  while (pos < m) {
    if (pos+i >= m) {
      prefix[a][e].push_front(m-pos);
    } else if (str[len-1-i] == s[m-1-(pos+i)]) {
      if (i != len) {
	++i;
	continue;
      }
    }
    if (tab[i] > -1) {
      pos = pos + i - tab[i];
      i = tab[i];
    } else {
      i = 0;
      pos = pos+1;
    }
  }
}

std::vector<int> expanded;

void expand(int a, int e) {
  if (e == -1) {
    expanded.push_back(a);
    return;
  }
  for (int i = 0; i < el[a][0]; ++i) {
    expand(h[a][i], e-1);
  }
}

void expand_search(int a, int e) {
  expanded.clear();
  expand(a, e);
  search(&expanded[0], expanded.size(), a, e);
}

void recurs(int i, int e) {
  if (el[i][e]) {
    return;
  }
  for (int j = 0; j < el[i][0]; ++j) {
    recurs(h[i][j], e-1);
    if (el[h[i][j]][e-1] < m) {
      if (el[i][e] <= m) {
	el[i][e] += el[h[i][j]][e-1];
      }
    } else {
      el[i][e] = m;
    }
    if (whole[h[i][j]][e-1]) {
      whole[i][e] = true;
      return;
    }
  }
  int seqlen = el[i][0];
  int first_elem = h[i][0];
  int last_elem = h[i][seqlen-1];
  if (el[i][e] < m) {
    expand_search(i, e);
  } else {
    // sprawdz czy jest w tym elemencie
    int off = 0;
    std::vector<int> match(m);
    for (int end : prefix[first_elem][e-1]) {
      match[end] = off+1;
    }
    for (int j = 1; j < seqlen; ++j) {
      for (int begin : suffix[h[i][j]][e-1]) {
	if (match[begin] == off+j) {
	  whole[i][e] = true;
	  return;
	}
      }
      int len = el[h[i][j]][e-1];
      if (len < m) {
	// może być w środku
	for (int begin : eh[h[i][j]][e-1]) {
	  if (match[begin] == off+j) {
	    match[begin+len] = off+j+1;
	  }
	}
      }
      for (int end : prefix[h[i][j]][e-1]) {
	match[end] = off+j+1;
      }
    }
    // policz sufiks
    off += seqlen+1;
    suffix[i][e] = suffix[first_elem][e-1];
    int total_len = el[first_elem][e-1];
    for (int begin : eh[first_elem][e-1]) {
      match[begin+total_len] = off+1;
    }
    for (int j = 1; j < seqlen; ++j) {
      int len = el[h[i][j]][e-1];
      for (int begin : suffix[h[i][j]][e-1]) {
	if (match[begin] == off+j) {
	  suffix[i][e].push_back(begin-total_len);
	}
      }
      total_len += len;
      if (total_len > m) {
	break;
      }
      bool added = false;
      for (int begin : eh[h[i][j]][e-1]) {
	if (match[begin] == off+j) {
	  match[begin+len] = off+j+1;
	  added = true;
	}
      }
      //if (!added)
	//break;
    }
    // policz prefix
    off += seqlen+1;
    prefix[i][e] = prefix[last_elem][e-1];
    total_len = el[last_elem][e-1];
    for (int begin : eh[last_elem][e-1]) {
      match[begin] = off+seqlen-2;
    }
    for (int j = seqlen-2; j >= 0; --j) {
      int len = el[h[i][j]][e-1];
      for (int end : prefix[h[i][j]][e-1]) {
	if (match[end] == off+j) {
	  prefix[i][e].push_back(end+total_len);
	}
      }
      total_len += len;
      if (total_len > m) {
	break;
      }
      bool added = false;
      for (int k = eh[h[i][j]][e-1].size()-1; k >= 0; --k) {
	int begin = eh[h[i][j]][e-1][k];
	if (match[begin+len] == off+j) {
	  match[begin] = off+j-1;
	  added = true;
	}
      }
     // if (!added)
	//break;
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &el[i][0]);
    for (int j = 0; j < el[i][0]; ++j) {
      scanf("%d", &h[i][j]);
    }
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", &s[i]);
  }
  for (int i = 1; i <= n; ++i) {
    expand_search(i, 0);
  }
  std::queue<std::pair<int, int>> q;
  int min = INF;
  d[1][0] = 1;
  q.push(std::make_pair(1, 0));
  while (!q.empty()) {
    int a = q.front().first;
    int b = q.front().second;
    int cur = d[a][b];
    if (cur > min)
      break;
    //fprintf(stderr, "%d %d %d\n", a, b, cur);
    q.pop();
    if (!b) {
      for (int i = 0; i < el[a][0]; ++i) {
	if (!d[h[a][i]][0]) {
	  d[h[a][i]][0] = cur+1;
	  q.push(std::make_pair(h[a][i], 0));
	}
      }
      for (int i = 1; i < el[a][0]; ++i) {
	if (!d[h[a][i-1]][h[a][i]]) {
	  d[h[a][i-1]][h[a][i]] = cur+1;
	  q.push(std::make_pair(h[a][i-1], h[a][i]));
	}
      }
      // whole check
      for (int i = 0; i < MAX_LOG_M; ++i) {
	if (whole[a][i]) {
	  min = std::min(min, cur+i+1);
	  break;
	}
      }
    } else {
      if (!d[h[a][el[a][0]-1]][h[b][0]]) {
	d[h[a][el[a][0]-1]][h[b][0]] = cur+1;
	q.push(std::make_pair(h[a][el[a][0]-1], h[b][0]));
      }
      for (int i = 0; i < MAX_LOG_M; ++i) {
	recurs(a, i);
	recurs(b, i);
	// FIXME optimize?
	int k = 0, l = 0;
	int asize = prefix[a][i].size();
	int bsize = suffix[b][i].size();
	while (k < asize && l < bsize) { 
	  //fprintf(stderr, "pref %d suff %d\n", prefix[a][i][k], suffix[b][i][l]);
	  for (; prefix[a][i][k] < suffix[b][i][l] && k < asize; ++k);
	  if (prefix[a][i][k] == suffix[b][i][l]) {
	    min = std::min(min, cur+i+1);
	  }
	  ++l;
	}
      }
    }
  }
  printf("%d\n", min == INF ? -1 : min);
}