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
#include <algorithm>
#include <cstdio>
#include <vector>

class Length {
 public:
  Length() : Length(0) {}
  Length(int length) : length_(length) {}
  Length(const Length& length) = default;
  Length(Length&& length) = default;
  Length& operator=(const Length& length) = default;
  Length& operator=(Length&& length) = default;

  Length& operator+=(const Length& length) {
    length_ = std::min(length_ + length.length_, 10000);
    return *this;
  }

  Length operator+(const Length& length) const {
    return Length(*this) += length;
  }

  bool operator<(const Length& length) const {
    return length_ < length.length_;
  }

  int ToInt() const {
    return length_;
  }

 private:
  int length_;
};

int n, m;
int l[505];
std::vector<int> H[505];
int pattern[1005];

class {
 public:
  const int infty = 23784923;
  int cell_distance[505];
  int pair_distance[505][505];

  void Compute() {
    Init();
    for (int i = 1; i < 11; i++) {
      current_distance = i;
      Explode();
    }
    /*
    for (int i = 1; i <= n; i++) {
      if (cell_distance[i] != infty) {
        printf("cell_distance[%d] = %d\n", i, cell_distance[i]);
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (pair_distance[i][j] != infty) {
          printf("pair_distance[%d][%d] = %d\n", i, j, pair_distance[i][j]);
        }
      }
    }
    */
  }

 private:
  struct {
    Length length[505];
    std::vector<Length> prefix_length[505];
    std::vector<Length> suffix_length[505];
    bool CanEnd[505][1005];
    bool CanStart[505][1005];
  } DataNumber1, DataNumber2, *now, *then;

  bool CanPrefixEnd[1005][1005];
  bool CanSuffixStart[1005][1005];

  int CellsThatEnd[1005];
  int CellsThatStart[1005];

  int current_distance;

  void AddCellDistance(int cell) {
    cell_distance[cell] = std::min(cell_distance[cell], current_distance);
  }

  void AddPairDistance(int cell_a, int cell_b) {
    pair_distance[cell_a][cell_b] = std::min(pair_distance[cell_a][cell_b],
                                             current_distance);
  }

  void Init() {
    now = &DataNumber1;
    then = &DataNumber2;
    for (int i = 1; i <= n; i++) {
      now->length[i] = 1;
      now->prefix_length[i].resize(l[i]);
      now->suffix_length[i].resize(l[i]);
      then->prefix_length[i].resize(l[i]);
      then->suffix_length[i].resize(l[i]);
    }
    for (int i = 0; i < m; i++) {
      now->CanEnd[pattern[i]][i] = true;
      now->CanStart[pattern[i]][i] = true;
    }
    for (int i = 1; i <= n; i++) {
      cell_distance[i] = infty;
      for (int j = 1; j <= n; j++) {
        pair_distance[i][j] = infty;
      }
    }
    if (m == 1) {
      cell_distance[pattern[0]] = 0;
    } else if (m == 2) {
      pair_distance[pattern[0]][pattern[1]] = 0;
    }
  }

  void Explode() {
    std::swap(now, then);
    for (int cell = 1; cell <= n; cell++) {
      MakeLengths(cell);
      MakeMatches(cell);
    }
    for (int position = 1; position < m; position++) {
      int end_counter = 0;
      int start_counter = 0;
      for (int cell = 1; cell <= n; cell++) {
        if (now->CanEnd[cell][position - 1]
            and now->length[cell].ToInt() >= position) {
          CellsThatEnd[end_counter++] = cell;
        }
        if (now->CanStart[cell][position]
            and now->length[cell].ToInt() >= m - position) {
          CellsThatStart[start_counter++] = cell;
        }
      }
      for (int end_i = 0; end_i < end_counter; end_i++) {
        for (int start_i = 0; start_i < start_counter; start_i++) {
          AddPairDistance(CellsThatEnd[end_i], CellsThatStart[start_i]);
        }
      }
    }
  }

  void MakeMatches(int cell) {
    auto can_prefix_end = [this, cell](int prefix, int position) -> bool {
      return prefix < 0 or position < 0 or CanPrefixEnd[prefix][position];
    };
    auto can_suffix_start = [this, cell](int suffix, int position) -> bool {
      return suffix >= l[cell] or position >= m
          or CanSuffixStart[suffix][position];
    };
    for (int prefix = 0; prefix < l[cell]; prefix++) {
      for (int position = 0; position < m; position++) {
        CanPrefixEnd[prefix][position]
            = then->CanEnd[H[cell][prefix]][position]
                and can_prefix_end(prefix - 1,
                    position - then->length[H[cell][prefix]].ToInt());
      }
    }
    for (int suffix = l[cell] - 1; suffix >= 0; suffix--) {
      for (int position = 0; position < m; position++) {
        CanSuffixStart[suffix][position]
            = then->CanStart[H[cell][suffix]][position]
                and can_suffix_start(suffix + 1,
                    position + then->length[H[cell][suffix]].ToInt());
      }
    }
    for (int position = 0; position < m; position++) {
      now->CanEnd[cell][position] = CanPrefixEnd[l[cell] - 1][position];
      now->CanStart[cell][position] = CanSuffixStart[0][position];
    }
    auto get_prefix_length = [this, cell](int prefix) -> Length {
      if (prefix < 0) {
        return Length();
      }
      return now->prefix_length[cell][prefix];
    };
    auto get_suffix_length = [this, cell](int suffix) -> Length {
      if (suffix >= l[cell]) {
        return Length();
      }
      return now->suffix_length[cell][suffix];
    };
    for (int position = 0; position <= m; position++) {
      for (int divide = 0; divide <= l[cell]; divide++) {
        if (can_prefix_end(divide - 1, position - 1)
            and can_suffix_start(divide, position)
            and get_prefix_length(divide - 1).ToInt() >= position
            and get_suffix_length(divide).ToInt() >= m - position) {
          AddCellDistance(cell);
        }
      }
    }
  }

  void MakeLengths(int cell) {
    now->length[cell] = 0;
    for (int i = 0; i < l[cell]; i++) {
      now->length[cell] += then->length[H[cell][i]];
      now->prefix_length[cell][i] = now->length[cell];
    }
    now->suffix_length[cell][l[cell] - 1] = then->length[H[cell][l[cell] - 1]];
    for (int i = l[cell] - 2; i >= 0; i--) {
      now->suffix_length[cell][i] = now->suffix_length[cell][i + 1]
          + then->length[H[cell][i]];
    }
  }
} Distances;

class {
 private:
  int p_, k_;
  std::pair<int, int> queue_[2000005];

  void Init() {
    p_ = k_ = 1000003;
  }

  void Add(int a, int b) {
    queue_[k_++] = {a, b};
  }

  void AddBefore(int a, int b) {
    queue_[--p_] = {a, b};
  }

  int A() {
    return queue_[p_].first;
  }

  int B() {
    return queue_[p_].second;
  }

  void Remove() {
    p_++;
  }

  bool Empty() {
    return p_ == k_;
  }

  bool cell_visited[505];
  bool pair_visited[505][505];

 public:
  int Run() {
    int result = Distances.infty;
    auto AddResult = [&result](int new_result) -> void {
      result = std::min(result, new_result);
    };
    auto VisitCellBefore = [this](int cell) -> void {
      if (!cell_visited[cell]) {
        cell_visited[cell] = true;
        AddBefore(cell, -1);
      }
    };
    auto VisitPair = [this](int cell_a, int cell_b) -> void {
      if (!pair_visited[cell_a][cell_b]) {
        pair_visited[cell_a][cell_b] = true;
        Add(cell_a, cell_b);
      }
    };
    Init();
    Add(1, -1);
    Add(-1, -1);
    int distance = 0;
    while (!Empty()) {
      int a = A();
      int b = B();
      Remove();
      if (a == -1) {  // Counter.
        distance++;
        if (!Empty()) {
          Add(-1, -1);
        }
      } else if (b == -1) {  // One cell.
        AddResult(distance + Distances.cell_distance[a]);
        for (int i = 1; i < l[a]; i++) {
          VisitPair(H[a][i - 1], H[a][i]);
        }
      } else {  // Pair of cells.
        AddResult(distance + Distances.pair_distance[a][b]);
        VisitCellBefore(a);
        VisitCellBefore(b);
        VisitPair(H[a][l[a] - 1], H[b][0]);
      }
    }
    return result;
  }
} Bfs;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &l[i]);
    for (int j = 1; j <= l[i]; j++) {
      int h;
      scanf("%d", &h);
      H[i].push_back(h);
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &pattern[i]);
  }
  Distances.Compute();
  int result = Bfs.Run();
  if (result == Distances.infty) {
    printf("-1\n");
  } else {
    printf("%d\n", result + 1);
  }
  return 0;
}