Under construction

Still being built. Things may change

Harry Potter and the Vector Spell

Interview

Time limit1sMemory limit256 MB

Summary
Each of N columns is a binary vector with exactly two 1s; find the rank of this M-by-N matrix over GF(2).
Level

Medium6 of 10

Topics
Graph, Union-find, Math, Greedy
Solved
No attempts yet

Problem

Harry Potter found one more strange spell in the Half Blood Prince's diary. The spell creates a binary vector of size MM. Harry is not the best magician yet, so the spell does not work perfectly and every vector it creates has exactly 2 non-zero elements.

Harry used the spell NN times and stood all of the created vectors up as columns, which gives a matrix with MM rows and NN columns.

In the Magical Matrix Theory class the professor asked Harry to compute the rank of that matrix. Help him.

Operations in Magical Matrix Theory follow these rules.

+01
001
110
×01
000
101

The rank of a matrix AA is the largest number of linearly independent columns of AA. The vectors in a set T={v1⃗,v2⃗,…,vk⃗}T = \{\vec{v_1}, \vec{v_2}, \dots, \vec{v_k}\} are linearly independent when the equation a1v1⃗+a2v2⃗+⋯+akvk⃗=0⃗a_1\vec{v_1} + a_2\vec{v_2} + \dots + a_k\vec{v_k} = \vec{0}, where ai∈{0,1}a_i \in \{0, 1\}, holds only for a1=a2=⋯=ak=0a_1 = a_2 = \dots = a_k = 0.

Input

The first line has two integers MM (the size of a vector) and NN (the number of vectors Harry created).

Each of the next MM lines has the format kik_i, c1c_1, c2c_2, …\dots, ckic_{k_i}. Here kik_i is the number of non-zero elements in row ii, and the next kik_i numbers are the indices cjc_j of the columns that are non-zero in that row (1≤cj≤N1 \le c_j \le N, j=1,…,kij = 1, \dots, k_i).

  • 1≤N≤1051 \le N \le 10^5
  • 2≤M≤1052 \le M \le 10^5
  • 0≤ki≤N0 \le k_i \le N
  • Every column has exactly 2 non-zero elements.

Output

Print the rank of the matrix on the first line.

Hint

In the first example Harry created three vectors.

v1⃗=(1,1,0),v2⃗=(0,1,1),v3⃗=(1,0,1)\vec{v_1} = (1, 1, 0), \quad \vec{v_2} = (0, 1, 1), \quad \vec{v_3} = (1, 0, 1)

The matrix is

[101110011]\begin{bmatrix} 1 & 0 & 1 \\ 1 & 1 & 0 \\ 0 & 1 & 1 \end{bmatrix}

but v1⃗+v2⃗+v3⃗=0⃗\vec{v_1} + \vec{v_2} + \vec{v_3} = \vec{0}.

Examples2

  1. Example 1

    Input
    3 3
    2 1 3
    2 1 2
    2 2 3
    
    Expected output
    2
    
  2. Example 2

    Input
    4 3
    3 1 2 3
    1 1
    1 2
    1 3
    
    Expected output
    3