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
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
#include <cassert>
#include <bitset>
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int R = 12;
const int ONE = 1;
const int INF = 2000000000;

int N, M;
int LONG = 1001;
deque<int> prefix[500][12];
deque<int> suffix[500][12];

bool prefix_match[500][12][1000];
bool suffix_match[500][12][1000];

list<int> matched_prefixes[12][1000];
list<int> matched_suffixes[12][1000];

int D[501][501];
void compute_times() {
  REP(i,N+1)REP(j,N+1) D[i][j] = INF;
  D[0][N] = 0;
  queue<PII> Q;
  Q.push(mp(0, N));
  while (!Q.empty()) {
    PII v = Q.front();
    Q.pop();
    vector<PII> next;
    deque<int>& evolution = prefix[v.st][ONE];
    if (v.nd == N) REP(k, evolution.size()) {
      next.pb(mp(evolution[k], N));
      if (k < evolution.size() - 1) next.pb(mp(evolution[k], evolution[k+1]));
    } else {
      next.pb(mp(*suffix[v.st][ONE].rbegin(), *prefix[v.nd][ONE].begin()));
    }
    
    FOREACH(it, next) {
      if (D[it->st][it->nd] != INF) continue;
      D[it->st][it->nd] = D[v.st][v.nd] + 1;
      Q.push(*it);
    }
  }
}

int S[1000];

int result = INF;

void check_full(int v, int r, int s) {
  REP(k,N) {
    REP(t, prefix[k][ONE].size()) if (prefix[k][ONE][t] == v) {    
      int s1 = s; int t1 = t;
      while (s1 < M) {
        if (t1 == prefix[k][ONE].size()) goto fail;
        int u = prefix[k][ONE][t1];
        if (!prefix_match[u][r][s1]) goto fail;
        
        ++t1;
        s1 += prefix[u][r].size();
      }
      
      s1 = s + prefix[v][r].size() - 1; t1 = t;
      while (s1 >= 0) {
        if (t1 < 0) goto fail;
        int u = prefix[k][ONE][t1];
        if (!suffix_match[u][r][s1]) goto fail;
        
        --t1;
        s1 -= suffix[u][r].size();
      }
      
      result = min(result, r + 1 + D[k][N]);
      fail:;
    }
  }    
}

void check_all_full() {
  REP(i,N)REP(r,R)REP(s,M) {
    if (prefix_match[i][r][s] && prefix[i][r].size() + s <= M) {
      check_full(i,r,s);
    }
  }
}

void check_all_partial() {
  REP(r,R)FOR(s,1,M)FOREACH(it, matched_prefixes[r][s])FOREACH(it2, matched_suffixes[r][s-1]) {
    int i = *it, j = *it2;
    if (prefix[i][r].size() >= M - s && suffix[j][r].size() >= s) {
      result = min(result, r + D[j][i]);
    }
  } 
}

int main() {
  scanf("%d%d", &N, &M);
  REP(i,N) {
    int s;
    scanf("%d", &s);
    REP(j, s) {
      int h;
      scanf("%d", &h);
      --h;
      prefix[i][ONE].pb(h);
      suffix[i][ONE].pb(h);
    }
    
    prefix[i][0].pb(i);
    suffix[i][0].pb(i);
  }
  
  REP(i,M) {
    scanf("%d", &S[i]);
    --S[i];
  }
  
  if (M == 1 && S[0] == 0) {
    printf("1\n");
    return 0;
  }
  
  FOR(r,2,R) {
    REP(i, N) {
      FOREACH(it, prefix[i][r-1])
        FOREACH(it2, prefix[*it][ONE]) {
          prefix[i][r].pb(*it2);
          if (prefix[i][r].size() == LONG) goto done_prefix;
        }
      done_prefix:;
      for (deque<int>::reverse_iterator it = suffix[i][r-1].rbegin(); it != suffix[i][r-1].rend(); ++it) {
        for (deque<int>::reverse_iterator it2 = suffix[*it][ONE].rbegin(); it2 != suffix[*it][ONE].rend(); ++it2) {
          suffix[i][r].push_front(*it2);
          if (suffix[i][r].size() == LONG) goto done_suffix;
        }
      }
      done_suffix:;
    }
  }
  
  REP(i,N)REP(r,R)REP(s,M) {
    // TODO: optimize
    suffix_match[i][r][s] = prefix_match[i][r][s] = true;

    int s1 = s;
    FOREACH(it, prefix[i][r]) {
      if (s1 == M) break;
      if (*it != S[s1++]) {
        prefix_match[i][r][s] = false;
        break;
      }
    }
    
    s1 = s;
    for (deque<int>::reverse_iterator it = suffix[i][r].rbegin(); it != suffix[i][r].rend(); ++it) {
      if (s1 < 0) break;
      if (*it != S[s1--]) {
        suffix_match[i][r][s] = false;
        break;
      }
    }
    
    if (prefix_match[i][r][s]) matched_prefixes[r][s].pb(i);
    if (suffix_match[i][r][s]) matched_suffixes[r][s].pb(i);
  }
 
  compute_times();
  check_all_full();
  check_all_partial();
  
  if (result == INF) printf("-1\n");
  else printf("%d\n", result + 1);
}