n, m = list(map(int, input().split()))

S = set(range(1, m+1))



for i in range(n):

    k, *a = list(map(int, input().split()))

    S &= set(a)



print((len(S)))

n, m = list(map(int, input().split()))



s = set(range(1, m+1))

for i in range(n):

    k, *a = list(map(int, input().split()))

    s &= set(a)



print((len(s)))

n, m = list(map(int, input().split()))
S = set(range(1, m + 1))
for i in range(n):
    k, *a = list(map(int, input().split()))
    S &= set(a)
print((len(S)))

n, m = list(map(int, input().split()))
s = set(range(1, m + 1))
for i in range(n):
    k, *a = list(map(int, input().split()))
    s &= set(a)
print((len(s)))

-S = set(range(1, m + 1)) +s = set(range(1, m + 1)) -    S &= set(a) -print((len(S))) +    s &= set(a) +print((len(s)))