def main():
    N, M = map(int, input().split())
    B = []
    for _ in range(M):
        A = [0] * (N + 1)
        ks = list(map(int, input().split()))
        for s in ks[1:]:
            A[s] = 1
        B.append(A)
    P = list(map(int, input().split()))
    ans = int('1' * N, 2)
    for A, p in zip(B, P):
        A[0] = p
        ans &= int(''.join(map(str, A)), 2)
    print(ans)


main()
