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

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)
#define PB push_back
#define ST first
#define ND second
#define INF 1000000001

using namespace std;

typedef unsigned long long int ULL;
typedef pair < int , int > PII;

typedef vector < int > VI;
typedef vector < bool > VB;
typedef vector < PII > VP;
typedef vector < ULL > VUL;
typedef vector < VI > VVI;

/*************************************************************************/

struct Info
{
    int m, len;
    bool hasPattern;
    VB pref;
    VB suff;
    VB pos;

    /* empty info */
    Info(int m, int len): m(m), len(len)
    {
        hasPattern = 0;

        pref.resize(m-1,0);
        suff.resize(m-1,0);
        pos.resize(m,0);
    }

    /* info for one character string */
    Info(int val, VI &S): Info(SIZE(S), 1)
    {
        int m = SIZE(S);
        hasPattern = (m == 1 && S[0] == val);

        if (m > 1 && S[0] == val)
            pref[0] = 1;

        if (m > 1 && S.back() == val)
            suff[0] = 1;

        FORE(i,S)
            pos[i] = (S[i] == val);
    }

    /* combines the info about two subsequent strings into one */
    Info operator+ (const Info &other) const
    {
        if (!len)
            return other;
        else if (!other.len)
            return *this;

        int sumLen = min(m, len + other.len);
        Info sum(m, sumLen);

        sum.hasPattern = hasPattern || other.hasPattern;

        FOR(i,0,m-2)
            if (pref[i] && other.suff[m - 2 - i])
                sum.hasPattern = 1;

        FOR(i,0,m-2)
        {
            if (i + 1 <= other.len)
                sum.pref[i] = other.pref[i];
            else
                sum.pref[i] = pref[i - other.len] && other.pos[i - other.len + 1];

            if (i + 1 <= len)
                sum.suff[i] = suff[i];
            else
                sum.suff[i] = other.suff[i - len] && pos[m - 1 - i];
        }

        FOR(i,0,m-1)
            sum.pos[i] = (i + len <= m) && pos[i] && (i + len == m || other.pos[i + len]);

        return sum;
    }
};

/*************************************************************************/

int pairToInd(int u, int v, int n)
    { return (u + 1) * n + v; }

PII indToPar(int ind, int n)
    { return { ind/n - 1, ind%n }; }

int lastInd(int n)
    { return pairToInd(n-1, n-1, n); }

/*************************************************************************/

VI BFS(int v, VVI &G)
{
    int n = SIZE(G);
    int last = lastInd(n);

    VI dist(last + 1, INF);
    dist[0] = 0;

    queue < int > Q;
    Q.push(0);

    while (!Q.empty())
    {
        int q = Q.front();
        Q.pop();

        VI next; /* neighbours */

        if (q < n)
        {
            next = G[q];

            FOR(i,1,SIZE(G[q])-1)
                next.PB( pairToInd(G[q][i-1], G[q][i], n) );
        }
        else
        {
            PII e = indToPar(q, n);
            next.PB( pairToInd(G[e.ST].back(), G[e.ND][0], n) );
        }

        for (int u : next)
            if (dist[u] == INF)
            {
                dist[u] = dist[q] + 1;
                Q.push(u);
            }
    }

    return dist;
}

/*************************************************************************/

VUL bitvecToLongvec(VB &V)
{
    VUL ans(1,0);
    int cnt = 0;

    for (bool bit : V)
    {
        ans.back() <<= 1;
        ans.back() |= bit;
        cnt++;

        if (cnt == 64)
        {
            ans.PB(0);
            cnt = 0;
        }
    }

    return ans;
}

/*
    for each one and two element string
    check if in not more than log m steps
    it can produce the pattern
*/
VI solve(VVI &H, VI &S)
{
    int n = SIZE(H),
        m = SIZE(S),
        last = lastInd(n);

    const int STEPS = 14;

    VI needSteps(last + 1, INF);

    vector < Info > I;
    FOR(i,0,n-1) I.PB({i, S});

    FOR(i,0,STEPS - 1)
    {
        /* check all elements for pattern */
        FOR(j,0,n-1)
            if (I[j].hasPattern)
                needSteps[j] = min(needSteps[j], i);

        /* generate pref and suff bitmasks */
        vector < VUL > pref, suff;

        FOR(j,0,n-1)
        {
            VB herePref = I[j].pref;
            VB hereSuff = I[j].suff;

            for (int it = 0; it < m - 2 - it; it++)
                swap(hereSuff[it], hereSuff[m - 2 - it]);

            pref.PB(bitvecToLongvec(herePref));
            suff.PB(bitvecToLongvec(hereSuff));
        }

        /* check all pairs for pattern */
        FOR(j,0,n-1)
        {
            VP toCheck;

            FOR(k,0,n-1)
            {
                int ind = pairToInd(j, k, n);

                if (needSteps[ind] == INF)
                    toCheck.PB( {k, ind} );
            }

            FORE(pos,pref[j])
            {
                ULL val = pref[j][pos];

                if (val) for (PII k : toCheck)
                    if (val & suff[k.ST][pos])
                        needSteps[k.ND] = i;
            }
        }

        /* update dp */
        vector < Info > temp;

        FOR(j,0,n-1)
        {
            Info agg(m,0);

            for (int v : H[j])
                agg = agg + I[v];

            temp.PB(agg);
        }

        I = temp;
    }

    return needSteps;
}

/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    int n, m;
    cin >> n >> m;

    VVI H(n);

    FOR(i,0,n-1)
    {
        int len;
        cin >> len;

        while (len--)
        {
            int v;
            cin >> v; v--;

            H[i].PB(v);
        }
    }

    VI S(m);

    FOR(i,0,m-1)
    {
        int v;
        cin >> v; v--;

        S[i] = v;
    }

    VI needSteps = solve(H, S);
    VI dist = BFS(0, H);

    int ans = INF;

    FOR(i,0,lastInd(n))
        ans = min(ans, dist[i] + needSteps[i]);

    if (ans == INF)
        cout << -1;
    else
        cout << 1 + ans;

    return 0;
}

/*************************************************************************/