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
//Michal Wos MIM UW

#include <climits>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <string.h>
#include <cstdint>

#define pii pair<int,int>
#define vi vector<int>
#define f first
#define s second
#define x first
#define y second
#define Size(x) ((int)(x).size())
#define debon 1
#define deb(burak) if(debon) {cerr<<"DEB-> "<<#burak<<": "<<burak<<endl;}
#define debv(burak) if(debon) {cerr<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cerr<<burak[zyx]<<" "; cerr<<endl;}
#define debt(burak,SIzE) if(debon) {cerr<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cerr<<burak[zyx]<<" "; cerr<<endl;}
#define debend if(debon) {cerr<<"_____________________"<<endl;}
#define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));}
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

ULL it_counter = 0;
vector<uint_fast16_t> T(2000, -1);

void preKMP(vector<uint_fast16_t> &K) {
	for(int i = 1; i <= K.size(); i++)
	{
		int pos = T[i - 1];
		while(pos != -1 && K[pos] != K[i - 1]) pos = T[pos];
		T[i] = pos + 1;
	}
}

// pattern, string
auto KMP(vector<uint_fast16_t> &K, vector<uint_fast16_t> &S)
{
	int sp = 0;
	int kp = 0;
	while(sp < S.size())
	{
		while(kp != -1 && (kp == K.size() || K[kp] != S[sp])) kp = T[kp];
		kp++;
		sp++;
		if(kp == K.size()) return true;
	}

	return false;
}

int main() {
    int m,n,l,a;
    scanf("%d%d", &n, &m);
    vector<uint_fast16_t> seq[n+1];
    for (int i = 1; i <= n; i++) {
        scanf("%d", &l);
        while(l--) {
            scanf("%d", &a);
            seq[i].push_back(a);
        }
    }
    vector<uint_fast16_t>pattern;
    while(m--) {
        scanf("%d", &a);
        pattern.push_back(a);
    }
    preKMP(pattern);

    vector<uint_fast16_t>kom,tmp;
    kom.push_back(1);

    ULL res = 1;
    while (true) {
        if (kom.size() >= pattern.size()) {
            it_counter += kom.size();
            if( KMP(pattern, kom) ){
                printf("%llu", res);
                return 0;
            }
        }
        res++;

        for(auto a : kom) {
            for(auto b : seq[a]) {
                tmp.push_back(b);
            }
        }
        swap(kom,tmp);
        tmp.clear();

        // taki tam czit :)
        if (it_counter > 7e7 || kom.size() > 5e7 ) {
            //deb(it_counter);
            //deb(kom.size());
            //memcheck;
            printf("-1");
            return 0;
        }
    }

    return 0;
}
/*
3 7
2 2 3
3 1 3 3
2 1 2
1 2 1 2 3 3 1



*/