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
#include <iostream>
#include <utility>
#include <vector>
using std::pair;
using std::vector;

constexpr int INF = 1000000000;

// Input
int alphabet, patternLen;
vector<int> pattern; // tiny
vector<vector<int>> expansions; // tiny

template<class T>
class Array2D {
 public:
  Array2D() : sz1(0), sz2(0) {}
  Array2D(int a, int b) : sz1(a), sz2(b), val(a*b) {}
  Array2D(int a, int b, T v) : sz1(a), sz2(b), val(a*b, v) {}
  T  operator()(int a, int b) const { return val[a * sz2 + b]; }
  T &operator()(int a, int b)       { return val[a * sz2 + b]; }
  void Assign(int a, int b, T v) {
    sz1 = a;
    sz2 = b;
    val.assign(a*b, v);
  }
  void swap(Array2D &arr) {
    std::swap(sz1, arr.sz1);
    std::swap(sz2, arr.sz2);
    val.swap(arr.val);
  }
 private:
  int sz1, sz2;
  vector<T> val;
};

void ReadInput() {
  using std::cin;
  cin.sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> alphabet >> patternLen;
  expansions.resize(alphabet);
  for (vector<int> &e : expansions) {
    int len; cin >> len;
    e.resize(len);
    for (int &x : e) {
      cin >> x;
      --x;
    }
  }
  pattern.resize(patternLen);
  for (int &x : pattern) {
    cin >> x;
    --x;
  }
}

vector<int> BuildKMP() {
  vector<int> KMP(patternLen+1);
  KMP[0] = -1;
  int t = -1;
  for(int len=1;len<=patternLen;++len) {
    while (t >= 0 && pattern[t] != pattern[len-1]) {
      t = KMP[t];
    }
    ++t;
    KMP[len] = t;
  }
  return KMP;
}

void BuildLevel0(Array2D<int> &sm) {
  vector<int> KMP = BuildKMP();
  for (int c = 0; c < alphabet; ++c) {
    for(int len = 0; len < patternLen; ++len) {
      int &r = sm(c,len);
      if (pattern[len] == c) {
        r = len+1;
      } else if(len==0) {
        r = 0;
      } else {
        r = sm(c, KMP[len]);
      }
    }
    sm(c, patternLen) = patternLen;
  }
}

void BuildNextLevel(Array2D<int> &nextSm, const Array2D<int> &prevSm) {
  for (int c = 0; c < alphabet; ++c) {
    for (int len = 0; len <= patternLen; ++len) {
      int t = len;
      for (int e : expansions[c]) {
        t = prevSm(e, t);
      }
      nextSm(c, len) = t;
    }
  }
}

void BuildEndgames(vector<int> &singletonEndgame,
                   Array2D<int> &doubletEndgame) {
  singletonEndgame.assign(alphabet, INF);
  doubletEndgame.Assign(alphabet, alphabet, INF);

  int level = 0;
  // (char, state) -> state
  Array2D<int> sm(alphabet, patternLen+1);  // 2 MB
  Array2D<int> nextSm(alphabet, patternLen+1); // 2 MB

  BuildLevel0(sm);
  for(;;) {
    for (int c = 0; c < alphabet; ++c) {
      int &se = singletonEndgame[c];
      if(se==INF && sm(c, 0) == patternLen) {
        se=level;
      }
      for (int d = 0; d < alphabet; ++d) {
        int &de = doubletEndgame(c,d);
        if (de==INF && sm(d, sm(c,0)) == patternLen) {
          de = level;
        }
      }
    }
    if ((1 << level) >= patternLen) break;
    ++level;
    BuildNextLevel(nextSm, sm);
    sm.swap(nextSm);
  }
}

int Bfs(const vector<int> &singletonEndgame, const Array2D<int> &doubletEndgame) {
  typedef pair<int,int> State;  // second == alphabet: singleton

  int res = INF;
  vector<State> st, st2; // 2MB + 2MB
  st.reserve(alphabet * (alphabet+1));
  st2.reserve(alphabet * (alphabet+1));
  Array2D<char> visited(alphabet, alphabet+1, false);

  int dist = 0;
  pair<int,int> start { 0, alphabet };
  st.push_back(start);
  visited(start.first, start.second) = true;

  while (!st.empty()) {
    while (!st.empty()) {
      State s = st.back(); st.pop_back();
      if (s.second == alphabet) {
        // singleton
        int goal = dist + singletonEndgame[s.first];
        if (goal < res) res = goal;
        const vector<int> &exp = expansions[s.first];
        for (int c : exp) {
          State s2 {c, alphabet};
          auto &vis = visited(s2.first, s2.second);
          if (vis) continue;
          vis = true;
          st2.push_back(s2);
        }
        for(int i=0;i<int(exp.size())-1;++i) {
          State s2 {exp[i], exp[i+1]};
          auto &vis = visited(s2.first, s2.second);
          if (vis) continue;
          vis = true;
          st2.push_back(s2);
        }
      } else {
        // doublet
        int goal = dist + doubletEndgame(s.first, s.second);
        if (goal < res) res = goal;
        State s2 { expansions[s.first].back(), expansions[s.second].front() };
        auto &vis = visited(s2.first, s2.second);
        if (vis) continue;
        vis = true;
        st2.push_back(s2);
      }
    }
    st.swap(st2);
    ++dist;
  }

  return res;
}

int Solve() {
  vector<int> singletonEndgame; // tiny
  Array2D<int> doubletEndgame;  // 1 MB
  BuildEndgames(singletonEndgame, doubletEndgame);
  return Bfs(singletonEndgame, doubletEndgame);
}

int main() {
  ReadInput();
  int res = Solve();
  if (res == INF) {
    std::cout << "-1\n";
  } else {
    std::cout << (res+1) << '\n';
  }
}