#include<stdio.h>
typedef int set[1025];
set u,g,temp;
int apartine(set s,int x)
{
return s[x];
}
void inter(set a,set b,set c)
{
int i;
c[0]=0;
for(i=1;i<=a[0];i++)
if(apartine(b,a[i]))c[++c[0]]=a[i];
}
void copy(set dest,set sursa)
{
int i;
for(i=0;i<=sursa[0];i++)
dest[i]=sursa[i];
}
void dif(set a,set b,set c)
{
int i;
c[0]=0;
for(i=1;i<=a[0];i++)
if(!apartine(b,a[i]))c[++c[0]]=a[i];
}
int main()
{
int n,m,k,r,st[1025],dr[1025],i,x;
freopen("balanta.in","r",stdin);
freopen("balanta.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)u[i]=g[i]=i,u[0]=g[0]=n;
while(m--)
{
scanf("%d",&k);
for(i=1;i<=n;i++)st[i]=dr[i]=0;
for(i=1;i<=k;i++)scanf("%d",&x),st[x]=1;
for(i=1;i<=k;i++)scanf("%d",&x),dr[x]=1;
st[0]=dr[0]=k;
scanf("%d",&r);
switch(r)
{
case 1:
inter(g,st,temp);
copy(g,temp);
inter(u,dr,temp);
copy(u,temp);
break;
case 2:
inter(g,dr,temp);
copy(g,temp);
inter(u,st,temp);
copy(u,temp);
break;
case 0:
dif(u,st,temp);
copy(u,temp);
dif(u,dr,temp);
copy(u,temp);
dif(g,st,temp);
copy(g,temp);
dif(g,dr,temp);
copy(g,temp);
}
}
if(u[0]==1&&g[0]==0)printf("%d",u[1]);
else if(u[0]==0&&g[0]==1)printf("%d",g[1]);
else if(u[0]==g[0]&&u[0]==1&&u[1]==g[1])printf("%d",u[1]);
else printf("0");
return 0;
}