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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include <iomanip>
#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define ALL(x) x.begin(), x.end()
#define VAR(a,b) __typeof (b) a = b
#define REP(i,n) for (int _n=(n), i=0; i<_n; ++i)
#define FOR(i,a,b) for (int _b=(b), i=(a); i<=_b; ++i)
#define FORD(i,a,b) for (int _b=(b), i=(a); i>=_b; --i)
#define FORE(i,a) for (VAR(i,a.begin ()); i!=a.end (); ++i) 
#define IN(x) int x; cin >> x
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

typedef vector<int> VI;
typedef long long LL;
typedef pair<int,int> PII;
typedef double LD;

int extended_euclid(int a, int b, int &p, int &q) {
  int a0 = a, b0 = b;
  p = 1; q = 0;
  int  r = 0, s = 1;
  int c, quot, new_r, new_s;

  while (b != 0){
    c = a % b;
    quot = lrint (floor (a/b));
    a = b;
    b = c;
    new_r = p - quot * r;
    new_s = q - quot * s;
    p = r; q = s;
    r = new_r; s = new_s;
  }
  int gcd = a;
  assert(gcd == p * a0 + q * b0);
  return gcd;
}

int solve_congruence(int a, int m, int b, int n) {
  assert(a >= 0);
  assert(a < m);
  assert(b >= 0);
  assert(b < n);
  int u, v;
  int gcd = extended_euclid(m, n, u, v);
  int lcm = m * n / gcd;
  if ((b - a) % gcd) return -1;
  v *= -1;
  assert(gcd == m * u - n * v);
  int x = b + n * (v * (b - a) / gcd);
  x = x % lcm;
  if (x < 0) x += lcm;
  assert(x % m == a);
  assert(x % n == b);
  assert(x >= 0);
  return x;
}

int first_passing(int start, int cycle, int pass_value) {
  assert(pass_value >= start);
  int steps_until = (pass_value - start + cycle - 1) / cycle;
  int res = start + steps_until * cycle;
  assert(res % cycle == start % cycle);
  assert(res >= pass_value);
  assert(res - cycle < pass_value);
  return res;
}

int first_common_element_of_arithmetic_sequences(int start1, int cycle1, int start2, int cycle2) {
  if (start1 >= start2) {
    swap(start1, start2);
    swap(cycle1, cycle2);
  }
  int orig_start1 = start1;
  start1 = first_passing(start1, cycle1, start2);
  if (start1 == start2) return start1;
  int con_res = solve_congruence(start1 - start2, cycle1, 0, cycle2);
  if (con_res == -1) return -1;
  int res = start2 + con_res;
  assert(res >= start2);
  assert(res >= start1);
  assert(res % cycle1 == start1 % cycle1);
  assert(res % cycle2 == start2 % cycle2);
  return res;
}

void test_extended_euclid() {
  int p, q;
  int gcd = extended_euclid(174, 18, p, q);
  assert(gcd == 174 * p + 18 * q);
}

void test_first_common_element_of_arithmetic_sequences() {
  auto t = [](int a, int b, int c, int d) {
    int res = first_common_element_of_arithmetic_sequences(a, b, c, d);
    return res;
  };
  t(7, 5, 3, 7);
  t(0, 3, 15, 5);
  t(0, 2, 1, 2);
  //int mod = 100;
  //REP(i,1000000) {
  //  int a = rand() % mod, b = rand() % mod + 1, c = rand() % mod, d = rand() % mod + 1;
  //  cout << a << " " << b << " " << c << " " << d << endl;
  //  int res = t(a,b,c,d);
  //  if (res == -1) res = mod * mod;
  //  REP(i, res) {
  //    if (i < a) continue;
  //    if (i < c) continue;
  //    if (i % b != a % b) continue;
  //    if (i % d != c % d) continue;
  //    assert(false);
  //  }
  //}
}

const int MAXN = 500, MAXM = 1000;

int n, m;

vector<VI> v;

VI s;

const int INF_LEN = int(1e4), MAX_STEPS = 13;
int len[MAX_STEPS][MAXN];

void calc_lens() {
  REP(cell, n) len[0][cell] = 1;
  FOR(steps,1,MAX_STEPS-1) REP(cell, n) {
    int& res = len[steps][cell];
    res = 0;
    for (auto child : v[cell]) {
      res += len[steps - 1][child];
      if (res >= INF_LEN) {
        res = INF_LEN;
        break;
      }
    }
  }
}

int steps_to_reach[MAXN];

void calc_steps_to_reach() {
  REP(cell, n) steps_to_reach[cell] = -1;
  steps_to_reach[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int cell = q.front();
    q.pop();
    for (auto child: v[cell])
      if (steps_to_reach[child] == -1) {
        steps_to_reach[child] = steps_to_reach[cell] + 1;
        q.push(child);
      }
  }
}

bool expansion_matches_prefix[MAX_STEPS][MAXM][MAXN];

void calc_expansion_matches_prefix() {
  REP(pos, m) REP(cell,n)
    expansion_matches_prefix[0][pos][cell] = cell == s[pos];
  FOR(steps,1,MAX_STEPS - 1) REP(pos, m) REP(cell, n) {
    int next_pos = pos;
    bool not_match = false;
    for (auto child : v[cell]) {
      if (!expansion_matches_prefix[steps - 1][next_pos][child]) {
        not_match = true;
        break;
      }
      next_pos += len[steps - 1][child];
      if (next_pos >= m) break;
    }
    expansion_matches_prefix[steps][pos][cell] = !not_match;
    //if (steps <= 3 && pos == 2)
    //  cout << steps << " " << pos << " " << cell << " " << expansion_matches_prefix[steps][pos][cell] << endl;
  }
}

bool expansion_matches_suffix[MAX_STEPS][MAXM][MAXN];

void calc_expansion_matches_suffix() {
  REP(pos, m) REP(cell,n)
    expansion_matches_suffix[0][pos][cell] = cell == s[pos];
  FOR(steps,1,MAX_STEPS - 1) REP(pos, m) REP(cell, n) {
    int next_pos = pos;
    bool not_match = false;
    for (auto child = v[cell].rbegin(); child != v[cell].rend(); ++child) {
      if (!expansion_matches_suffix[steps - 1][next_pos][*child]) {
        not_match = true;
        break;
      }
      next_pos -= len[steps - 1][*child];
      if (next_pos < 0) break;
    }
    expansion_matches_suffix[steps][pos][cell] = !not_match;
    //cout << steps << " " << pos << " " << cell << " " << expansion_matches_suffix[steps][pos][cell] << endl;
  }
}

int try_to_generate_suffix_from_few_expansions(int cell, int pos) {
  FOR(steps, 0, MAX_STEPS - 1)
    if (expansion_matches_suffix[steps][pos][cell] && pos - len[steps][cell] < 0)
      return steps;
  return -1;
}

int min_steps_to_generate_suffix[MAXM][MAXN];

void calc_min_steps_to_generate_suffix() {
  VI cur_res(n);
  REP(pos, m) {
    REP(cell, n)
      cur_res[cell] = try_to_generate_suffix_from_few_expansions(cell, pos);
    bool improved;
    do {
      improved = false;
      REP(cell, n) {
        int last_child = v[cell].back();
        if (cur_res[last_child] != -1 && (cur_res[cell] == -1 || cur_res[last_child] + 1 < cur_res[cell])) {
          cur_res[cell] = cur_res[last_child] + 1;
          improved = true;
        }
      }
    } while (improved);
    REP(cell, n) min_steps_to_generate_suffix[pos][cell] = cur_res[cell];

    //cout << "pos = " << pos << endl;
    //REP(cell, n) cout << cell << " " << cur_res[cell] << endl;
    //cout << endl << endl << endl;
  }
}

int try_to_generate_prefix_from_few_expansions(int cell, int pos) {
  FOR(steps, 0, MAX_STEPS - 1)
    if (expansion_matches_prefix[steps][pos][cell] && pos + len[steps][cell] >= m)
      return steps;
  return -1;
}

int min_steps_to_generate_prefix[MAXM][MAXN];

void calc_min_steps_to_generate_prefix() {
  VI cur_res(n);
  REP(pos, m) {
    REP(cell, n)
      cur_res[cell] = try_to_generate_prefix_from_few_expansions(cell, pos);
    bool improved;
    do {
      improved = false;
      REP(cell, n) {
        int first_child = v[cell][0];
        if (cur_res[first_child] != -1 && (cur_res[cell] == -1 || cur_res[first_child] + 1 < cur_res[cell])) {
          cur_res[cell] = cur_res[first_child] + 1;
          improved = true;
        }
      }
    } while (improved);
    REP(cell, n) min_steps_to_generate_prefix[pos][cell] = cur_res[cell];

    //cout << "pos = " << pos << endl;
    //REP(cell, n) cout << cell << " " << cur_res[cell] << endl;
    //cout << endl << endl << endl;
  }
}

bool sequence_matches_suffix_after_steps(const VI& seq, int steps, int pos) {
  assert(steps < MAX_STEPS);
  for (auto cell = seq.rbegin(); cell != seq.rend(); ++cell) {
    if (pos < 0) break;
    if (!expansion_matches_suffix[steps][pos][*cell]) return false;
    pos -= len[steps][*cell];
  }
  return pos < 0;
}

int suffix_generate_itself_cycle[MAXM];

void calc_suffix_generate_itself_cycle() {
  VI prefix;
  REP(pos, m) {
    prefix.PB(s[pos]);
    int& res = suffix_generate_itself_cycle[pos];
    res = -1;
    suffix_generate_itself_cycle[pos] = -1;
    FOR(steps, 1, MAX_STEPS - 1)
      if (sequence_matches_suffix_after_steps(prefix, steps, pos)) {
        res = steps;
        break;
      }
    int from_single = min_steps_to_generate_suffix[pos][prefix.back()];
    if (from_single == 0) {
      int last_child = v[prefix.back()].back();
      from_single = min_steps_to_generate_suffix[pos][last_child];
      if (from_single == -1) continue;
      ++from_single;
    }
    if (from_single == -1) continue;
    if (res == -1 || from_single < res) res = from_single;
    //cout << pos << " " << suffix_generate_itself_cycle[pos] << endl;
  }
}

bool sequence_matches_prefix_after_steps(const VI& seq, int steps, int pos) {
  assert(steps < MAX_STEPS);
  FORE(cell, seq) {
    if (pos >= m) break;
    if (!expansion_matches_prefix[steps][pos][*cell]) return false;
    pos += len[steps][*cell];
  }
  return pos >= m;
}

int prefix_generate_itself_cycle[MAXM];

void calc_prefix_generate_itself_cycle() {
  REP(pos, m) {
    VI suffix;
    for (int i = pos; i < m; ++i) suffix.PB(s[i]);
    int& res = prefix_generate_itself_cycle[pos];
    res = -1;
    FOR(steps, 1, MAX_STEPS - 1)
      if (sequence_matches_prefix_after_steps(suffix, steps, pos)) {
        res = steps;
        break;
      }
    int from_single = min_steps_to_generate_prefix[pos][suffix[0]];
    if (from_single == 0) {
      int first_child = v[suffix[0]][0];
      from_single = min_steps_to_generate_prefix[pos][first_child];
      if (from_single == -1) continue;
      ++from_single;
    }
    if (from_single == -1) continue;
    if (res == -1 || from_single < res) res = from_single;
    //cout << pos << " " << generate_itself_cycle[pos] << endl;
  }
}

int res = -1;

void update_res(int cand) {
  if (res == -1 || cand < res) res = cand;
}

bool covers_in_zero_steps(int cell) {
  return m == 1 && cell == s[0];
}

bool covers_in_single_step(int cell) {
  int prod_size = v[cell].size();
  for (int start = 0; start + m < prod_size; ++start) {
    bool ok = true;
    for (int i = 0; i < m; ++i)
      if (v[cell][start + i] != s[i]) {
        ok = false;
        break;
      }
    if (ok) return true;
  }
  return false;
}

void try_to_cover_as_prefix(int cell) {
  int min_steps = min_steps_to_generate_prefix[0][cell];
  if (min_steps != -1) update_res(steps_to_reach[cell] + min_steps);
}

void try_to_cover_as_suffix(int cell) {
  int min_steps = min_steps_to_generate_suffix[m - 1][cell];
  if (min_steps != -1) update_res(steps_to_reach[cell] + min_steps);
}

void try_to_match_production(int cell) {
  VI prefix, suffix = v[cell];
  int to_reach = steps_to_reach[cell] + 1;
  while (suffix.size() > 1) {
    prefix.PB(suffix[0]);
    suffix.erase(suffix.begin());
    FOR(pos, 1, m - 1) {
      bool found_steps = false;
      REP(steps, MAX_STEPS - 1) {
        bool suffix_match =  sequence_matches_suffix_after_steps(prefix, steps, pos - 1),
             prefix_match = sequence_matches_prefix_after_steps(suffix, steps, pos);
        //cout << steps << " " << suffix_match << " " << prefix_match << endl;
        if (suffix_match && prefix_match) {
          found_steps = true;
          update_res(to_reach + steps);
          break;
        }
      }
      if (found_steps) continue;
      int min_steps_for_prefix = min_steps_to_generate_prefix[pos][suffix[0]];
      if (min_steps_for_prefix == -1) continue;
      int min_steps_for_suffix = min_steps_to_generate_suffix[pos - 1][prefix.back()];
      if (min_steps_for_suffix == -1) continue;
      int cycle_for_prefix = prefix_generate_itself_cycle[pos],
          cycle_for_suffix = suffix_generate_itself_cycle[pos - 1];
      //cout << "hello " << min_steps_for_prefix << " " << cycle_for_prefix << " " <<
      //  min_steps_for_suffix << " " << cycle_for_suffix << endl;
      if (cycle_for_suffix == -1) {
        swap(cycle_for_suffix, cycle_for_prefix);
        swap(min_steps_for_suffix, min_steps_for_prefix);
      }
      if (cycle_for_prefix == -1) {
        if (cycle_for_suffix == -1) {
          if (min_steps_for_suffix == min_steps_for_prefix)
            update_res(to_reach + min_steps_for_prefix);
          continue;
        }
        if (min_steps_for_suffix > min_steps_for_prefix) continue;
        if (min_steps_for_suffix % cycle_for_suffix == min_steps_for_prefix % cycle_for_suffix)
          update_res(to_reach + min_steps_for_prefix);
        continue;
      }
      assert(cycle_for_suffix != -1);
      assert(cycle_for_prefix != -1);
      int first_common = first_common_element_of_arithmetic_sequences(min_steps_for_suffix,
          cycle_for_suffix, min_steps_for_prefix, cycle_for_prefix);
      if (first_common == -1) continue;
      update_res(to_reach + first_common);
    }
  }
}

void solve() {
  REP(cell, n) if (steps_to_reach[cell] != -1) {
    if (covers_in_zero_steps(cell)) update_res(steps_to_reach[cell]);
    if (covers_in_single_step(cell)) update_res(steps_to_reach[cell] + 1);
    try_to_cover_as_prefix(cell);
    try_to_cover_as_suffix(cell);
    try_to_match_production(cell);
  }
}

void read_input() {
  cin >> n >> m;
  v.resize(n);
  for (auto& it : v) {
    IN(l);
    it.resize(l);
    for (auto& jt : it) {
      cin >> jt;
      --jt;
    }
  }
  s.resize(m);
  for (auto& it : s) {
    cin >> it;
    --it;
  }
}

void debug() {
  VI suffix(1, 3);
  cout << expansion_matches_prefix[3][2][2] << endl;
  cout << sequence_matches_prefix_after_steps(suffix, 2, 2) << endl;
  exit(0);
}

int main() {
  ios_base::sync_with_stdio(0);
  cout.setf(ios::fixed);
  test_extended_euclid();
  test_first_common_element_of_arithmetic_sequences();
  read_input();
  calc_lens();
  calc_expansion_matches_prefix();
  calc_expansion_matches_suffix();
  calc_steps_to_reach();
  calc_min_steps_to_generate_prefix();
  calc_min_steps_to_generate_suffix();
  calc_prefix_generate_itself_cycle();
  calc_suffix_generate_itself_cycle();
  //debug();
  solve();
  if (res != -1) ++res;
  cout << res << endl;
  return 0;
}