#include <iostream>
#include <stdio.h>
const int L = 2000;
using namespace std;
int main()
{
freopen("balanta.in", "r", stdin);
freopen("balanta.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
bool valid[L];
for(int i = 1; i < L; i ++)
valid[i] = 1;
int n, m, q, ban[L], x;
cin >> n >> m;
for(int i = 0; i < m; i ++)
{
cin >> q;
for(int j = 1; j <= n; j ++)
ban[j] = 0;
for(int j = 0; j < q; j ++)
{
cin >> x;
ban[x] = 1;
}
for(int j = 0; j < q; j ++)
{
cin >> x;
ban[x] = 2;
}
cin >> x;
if(x == 0)
{
for(int j = 1; j <= n; j ++)
{
if(ban[j])
valid[j] = 0;
}
}
if(x == 1)
{
for(int j = 1; j <= n; j ++)
{
if(ban[j] == 2)
valid[j] = 0;
}
}
if(x == 2)
{
for(int j = 1; j <= n; j ++)
{
if(ban[j] == 1)
valid[j] = 0;
}
}
}
int nr = 0, poz;
for(int i = 1; i <= n; i ++)
{
if(valid[i])
{
nr ++;
poz = i;
}
}
if(nr == 1)
cout << poz;
else cout << 0;
return 0;
}