Party Invitations

Time limit1sMemory limit128 MB

Summary
Inviting cow 1 forces whole groups once all but one member is in; find the smallest set of cows that must be invited.
Level

Medium7 of 10

Topics
Graph, BFS, Hash map, Implementation
Solved
No attempts yet

Problem

Farmer John is throwing a party and wants to invite some of his cows to show them how much he cares about his herd. Remembering all too well the disaster that resulted the last time he invited too many cows, he also wants to invite as few cows as possible.

Among FJ's cows there are certain groups of friends that are hard to separate. For any such group of size kk, if FJ invites at least k−1k-1 of the cows in the group, then he must invite the final cow as well, thereby including the entire group. Groups can be of any size and may overlap, although no two groups contain exactly the same set of members. The sum of all group sizes is at most 250,000250{,}000.

The cows are numbered 11 through NN (with NN at most 1,000,0001{,}000{,}000), and FJ has decided that he must start by inviting cow 11. Given the groups among FJ's cows, determine the minimum number of cows FJ must invite to his party.

Input

  • Line 1: Two space-separated integers NN (the number of cows) and GG (the number of groups).
  • Lines 2…1+G2 \dots 1+G: Each line describes a group. It begins with an integer SS, the size of the group, followed by the SS cows in the group (each an integer from 11 to NN).

Output

  • Line 1: The minimum number of cows FJ must invite to his party.

Hint

In the sample there are 1010 cows and 44 groups; the first group contains cows 11 and 33.

In addition to cow 11, FJ must invite cow 33 (because of the first group), cow 44 (because of the second group), and cow 22 (because of the last group), for a total of 44 cows.

Examples1

  1. Example 1

    Input
    10 4
    2 1 3
    2 3 4
    6 1 2 3 4 6 7
    4 4 3 2 1
    
    Expected output
    4