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
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
#define MAXN 507
vector<int> rep[MAXN];
int n, m, temp_a, temp_b;
vector<int> s;
vector<int> status;
bool done(){
    for(int i = 0; i + s.size()-1 < status.size(); i++){
        bool worked = true;
        for(int j = 0; j < s.size(); j++){
            if (status[i + j] != s[j]){
                worked = false;
                break;
            }
        }
        if (worked) return true;
    }
    return false;
}
void simulate(){
    vector<int> tmp;
    for(int i = 0; i < status.size(); i++){
        for(int j = 0; j < rep[status[i]].size(); j++)
            tmp.push_back(rep[status[i]][j]);
    }
    status = tmp;
}

int calc() {
    int res = 0;
    while (!done()){
        simulate();
        res++;
    }
    return res;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++){
        scanf("%d",&temp_a);
        while(temp_a--){
            scanf("%d",&temp_b);
            temp_b--;
            rep[i].push_back(temp_b);
        }
    }
    while(m--){
        scanf("%d",&temp_b);
        s.push_back(temp_b - 1);
    }
    status.push_back(0);
    printf("%d\n", 1+ calc());
}