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 <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

class Node
{
public:
	vector<int> to;
};

bool dfs(int p, int T, vector<Node>& graph, vector<vector<int> >& flow, vector<int>& level, vector<bool>& finished)
{
	if(p==T) return true;
	if(finished[p]) return false;;
	
	finished[p]=true;
	
	for(int i=0; i<graph[p].to.size(); i++)
	{
		int next=graph[p].to[i];
		if(level[p]>=level[next]) continue;
		if(flow[p][next]) continue;
		
		if(dfs(next, T, graph, flow, level, finished))
		{
			finished[p]=false;
			flow[p][next]++;
			flow[next][p]--;
			return true;
		}
	}
	return false;
}

int dinic(int S, int T, vector<Node>& graph, vector<vector<int> >& flow)
{
	bool end=false;
	int total=0;
	while(!end)
	{
		end=true;
		vector<int> level(graph.size(), -1);
		level[S]=0;
		
		queue<int> q;
		q.push(S);
		while(!q.empty())
		{
			int n=q.front(); q.pop();
			for(int i=0; i<graph[n].to.size(); i++)
			{
				int next=graph[n].to[i];
				if(level[next]!=-1) continue;
				if(flow[n][next]) continue;
				level[next]=level[n]+1;
				q.push(next);
			}
		}
		
		if(level[T]==-1) break;
		
		vector<bool> finished(graph.size());
		while(1)
		{
			if(dfs(S,T,graph,flow,level,finished))
			{
				total++;
				end=false;
			}
			else break;
		}
	}
	
	return total;
}

int main()
{
	int N,M;
	while(scanf("%d", &N), N)
	{
		map<int, int> d;
		vector<Node> graph;
		int cnt=N+2;
		scanf("%d", &M);
		graph.resize(N+2+M);
		vector<vector<int> > flow(N+2+M, vector<int>(N+2+M));
		for(int i=0; i<M; i++)
		{
			int t;
			scanf("%d", &t);
			if(d.count(t)==0) 
			{
				d[t]=cnt;
				cnt++;
			}
			
			int id=d[t];
			graph[0].to.push_back(id);
		}
		for(int i=0; i<N; i++)
		{
			scanf("%d", &M);
			while(M--)
			{
				int t;
				scanf("%d", &t);
				if(d.count(t)==0) continue;
				int id=d[t];
				graph[id].to.push_back(i+2);
				graph[i+2].to.push_back(id);
				flow[i+2][id]=1;
			}
			graph[i+2].to.push_back(1);
		}
		
		printf("%d\n", dinic(0,1,graph,flow));
	}
	
}