#include<bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define mp make_pair
#define eb emplace_back
#define pb push_back
typedef pair<int,int> pii;
const int mxn=1e6+5;
vector<int> adjl[mxn];
bool vis[mxn];
bool col[mxn];
int b,w;
int maxDeg;
void dfs(int nd,int cc=0){
maxDeg = max(maxDeg, (int)nd.size());
col[nd]=cc;
if(col[nd])b++;
else w++;
vis[nd]=1;
for(int x:adjl[nd]){
if(vis[x]){
//assert(col[x]!=col[nd]);
continue;
}
dfs(x,1-cc);
}
}
vector<pii> tt[mxn],cc[mxn];//each topic/contest, authors they're connected to
vector<pair<int,pii> > trips;
vector<int> nodes[mxn];
int32_t main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int a,c,t;
cin>>a>>c>>t;
int cur=1;
for(int i=1;i<=a;i++){
int l;cin>>l;
for(int j=0;j<l;j++){
int a;cin>>a;
//author i -> contest a
cc[a].eb(i,cur++);
}
}
for(int i=1;i<=a;i++){
int l;cin>>l;
for(int j=0;j<l;j++){
int a;cin>>a;
//author i -> topic a
tt[a].eb(i,cur++);
}
}
int xx=1;
for(int i=1;i<=c;i++){
int l;cin>>l;
for(int j=0;j<l;j++){
int a;cin>>a;
//cur: contest i -> topic a
for(pii x:cc[i]){
for(pii y:tt[a]){
if(x.f==y.f){
//triangle: author, contest, topic
nodes[x.s].pb(trips.size());
nodes[y.s].pb(trips.size());
nodes[cur].pb(trips.size());
trips.eb(x.f,mp(i,a));
}
}
}
cur++;
}
}
// for(auto x:trips){
// cout<<x.f<<' '<<x.s.f<<' '<<x.s.s<<'\n';
// }
for(int i=1;i<cur;i++){
assert(nodes[i].size()<=2);
if(nodes[i].size()==2){
int u=nodes[i][0];
int v=nodes[i][1];
adjl[u].pb(v);
adjl[v].pb(u);
// cout<<u<<' '<<v<<'\n';
}
}
int ans=0;
for(int i=0;i<trips.size();i++){
if(vis[i])continue;
b=w=0;
dfs(i);
if(b<w)swap(b,w);
ans+=b;
if (maxDeg >= 3) {
assert(b + w <= 8);
}
}
cout<<ans;
}