Problem: Isaac H. Ives attended an international student party and made a lot of girl friends (as many other persons expected). To strike up a good friendship with them, he decided to have dates with them. However, it is hard for him to schedule dates because he made so many friends. Thus he decided to find the best schedule using a computer program. The most important criterion in scheduling is how many different girl friends he will date. Of course, the more friends he will date, the better the schedule is. However, though he has the ability to write a program finding the best schedule, he doesn’t have enough time to write it.

Your task is to write a program to find the best schedule instead of him.



Input

The input consists of a series of data sets. The first line of each data set contains a single positive integer N (N ≤ 1,000) that represents the number of persons Isaac made friends with in the party. The next line gives Isaac’s schedule, followed by N lines that give his new friends’ schedules. Each schedule consists of a positive integer M that represents the number of available days followed by M positive integers each of which represents an available day.

The input is terminated by a line that contains a single zero. This is not part of data sets and should not be processed.

Output

For each data set, print a line that contains the maximum number of girl friends Isaac can have dates with.

Example

Input

3
3 1 3 5
2 1 4
4 1 2 3 6
1 3
0


Output

2

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

struct edge{int to,cap,rev;};

const int MAX_V = 2000;
const int F_INF = 19191919;
vector<edge> G[MAX_V];
int lv[MAX_V], it[MAX_V];

void add_edge(int from, int to, int cap){
    G[from].pb({to,cap,(int)G[to].size()});
    G[to].pb({from,0,(int)G[from].size()-1});
}

void dinic_bfs(int s){
    fill(lv,lv+MAX_V,-1);
    queue<int> que;
    lv[s]=0;
    que.push(s);
    while(!que.empty()){
        int v = que.front(); que.pop();
        rep(i,G[v].size()){
            edge &e = G[v][i];
            if(e.cap>0 && lv[e.to]<0){
                lv[e.to] = lv[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dinic_dfs(int v, int t, int f){
    if(v==t) return f;
    for(int &i=it[v]; i<G[v].size(); ++i){
        edge &e = G[v][i];
        if(e.cap>0 && lv[v]<lv[e.to]){
            int d = dinic_dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t){
    int flow = 0;
    while(1){
        dinic_bfs(s);
        if(lv[t]<0) return flow;
        fill(it,it+MAX_V,0);
        int f;
        while((f=dinic_dfs(s,t,F_INF))>0) flow+=f;
    }
}

int main()
{
    int n;
    while(scanf(" %d", &n),n)
    {
        rep(i,MAX_V) G[i].clear();

        unordered_map<int,int> m;
        int M = 0;

        int ISSAC;
        scanf(" %d", &ISSAC);
        while(ISSAC--)
        {
            int day;
            scanf(" %d", &day);
            if(!m.count(day)) m[day] = M++;
        }

        vector<int> k(n);
        vector<vector<int>> s(n);

        rep(i,n)
        {
            scanf(" %d", &k[i]);
            rep(j,k[i])
            {
                int day;
                scanf(" %d", &day);
                if(m.count(day)) s[i].pb(m[day]);
            }
            k[i] = s[i].size();
        }

        int S = n+M, T = S+1;
        rep(i,n)
        {
            add_edge(S,i,1);
            rep(j,k[i]) add_edge(i,n+s[i][j],1);
        }
        rep(i,M) add_edge(n+i,T,1);

        printf("%d\n", max_flow(S,T));
    }
    return 0;
}