Under construction

Still being built. Things may change

Easter Eggs

Interview

Time limit1sMemory limit1024 MB

Summary
Each child passes eggs along fixed wish edges, and every child is wanted by at most one other. Find the most children who can get an egg when the Bunny starts with M eggs.
Level

Medium6 of 10

Topics
Graph, Tree, Greedy, DFS
Solved
No attempts yet

Problem

The Easter Bunny has M painted eggs to give to N children. The Bunny may not have enough eggs for every child. However, a child who receives an egg can paint it and give it to another child, whether the egg came from the Bunny or from another child. An egg can only be given to a child who does not have one yet. The Bunny knows which child each child wants to give eggs to.

Look at this example with five children. The arrows show whom each child would want to give an egg to after receiving one:

If the Bunny gives an egg to the second child, that child paints it and gives one to the third child and one to the first child. The first child gives one to the fourth child. The fourth child wants to give one to the second child, but does not, because the second child already has an egg. The Bunny gave away only one egg, yet four children end up with eggs.

The Bunny realized that the total number of children who receive eggs depends on which children the Bunny gives eggs to. Find the maximum number of children who can receive eggs if the Easter Bunny distributes its eggs wisely.

Input

The first line contains the number of children N and the number of eggs M that the Bunny has. Next come N lines, one for each child. The line for the i-th child starts with an integer k, followed by k integers on the same line: the numbers of the children that the i-th child wants to give an egg to after receiving one. Each child is wanted by at most one child. A child never wants to give an egg to themselves.

Output

Print one integer on the only line: the maximum number of children who receive eggs.

Constraints

  • 1 ≤ N ≤ 100 000
  • 1 ≤ M ≤ N

Examples2

  1. Example 1

    Input
    3 2
    2 2 3
    0
    0
    
    Expected output
    3
    
  2. Example 2

    Input
    5 1
    1 4
    2 1 3
    0
    1 2
    0
    
    Expected output
    4