Zoo

Time limit2sMemory limit128 MB

Summary
Choose cages to empty on a circle so that the most children watching 5-cage arcs become happy, where each child needs one feared animal removed or one liked animal kept.
Level

Hard9 of 10

Topics
Dynamic programming, Bit manipulation, Sliding window, Brute force
Solved
No attempts yet

Problem

A new circular zoo, the pride of the Asia-Pacific region, has just been built on a small island in the Pacific. Its animal cages are arranged in one big circle, and each cage holds a single, distinct animal.

The goal is to make as many visiting children as possible enjoy their visit. This is not easy: some children love certain animals while others are frightened by them. For example, Alex loves monkeys and koalas because they are cute but is scared of lions because of their sharp teeth, while Polly loves lions for their beautiful manes but dislikes koalas because of their awful smell.

We may move some of the animals that children fear to another zoo (that is, empty those cages). But moving too many is bad, because then there would be nothing left to look at. We want to choose which animals to move so that as many children as possible are happy.

Each child stands outside the circle and watches only the animals in the 5 consecutive cages directly in front of them. For each child, the list of animals they fear and the list they like are given. A child is happy if at least one of the following holds:

  • at least one of the animals they fear (among the ones they watch) has been moved away;
  • at least one of the animals they like (among the ones they watch) still remains.

For example, suppose five children are described by the table below.

ChildWatched cagesFeared cagesLiked cages
Alex2, 3, 4, 5, 642, 6
Polly3, 4, 5, 6, 764
Chaitanya6, 7, 8, 9, 1096, 8
Hwan8, 9, 10, 11, 12912
Ka-Shu12, 13, 14, 1, 212, 13, 2(none)
  • If the animals in cages 4 and 12 are moved: Alex and Ka-Shu are happy because a feared animal was moved, and Chaitanya is happy because the liked animals in cages 6 and 8 remain. Polly and Hwan are not happy, because all of their liked animals were moved and none of their feared animals were. So 3 children are happy.
  • If the animals in cages 4 and 6 are moved: Alex and Polly are happy because a feared animal was moved; Chaitanya is happy because the liked animal in cage 8 remains, and Hwan because the liked animal in cage 12 remains. Only Ka-Shu is unhappy, so 4 children are happy.
  • If instead only the animal in cage 13 is moved: Ka-Shu is happy because a feared animal was moved, and each of the other four children still has at least one liked animal remaining, so all are happy. This gives the maximum of 5 happy children.

Find the maximum number of children that can be happy at the same time.

Input

The first line contains two integers NN and CC. NN (10≤N≤1000010 \le N \le 10000) is the number of cages and CC (1≤C≤500001 \le C \le 50000) is the number of children. The cages are numbered 1,2,…,N1, 2, \ldots, N clockwise around the circle.

Then CC lines follow, each describing one child's watched cages, feared animals, and liked animals in the following format.

E F L X1 X2 ... XF Y1 Y2 ... YL

where:

  • EE is the number of the first cage the child watches (1≤E≤N1 \le E \le N); that is, the child watches cages E,E+1,E+2,E+3,E+4E, E+1, E+2, E+3, E+4. If a number exceeds NN it wraps back to 11. For example, if N=14N = 14 and E=13E = 13, the watched cages are 13,14,1,2,313, 14, 1, 2, 3.
  • FF is the number of feared animals and LL is the number of liked animals.
  • X1,…,XFX_1, \ldots, X_F are the cage numbers of the feared animals (1≤Xi≤N1 \le X_i \le N).
  • Y1,…,YLY_1, \ldots, Y_L are the cage numbers of the liked animals (1≤Yi≤N1 \le Y_i \le N).

X1,…,XF,Y1,…,YLX_1, \ldots, X_F, Y_1, \ldots, Y_L are all distinct, and every one of them is a cage that this child watches.

The children are given in nondecreasing order of EE (the child with the smallest EE first, the largest EE last). Note that two or more children may share the same value of EE.

Output

Print a single integer: the maximum number of children that can be happy at the same time.

Examples3

  1. Example 1

    Input
    14 5
    2 1 2 4 2 6
    3 1 1 6 4
    6 1 2 9 6 8
    8 1 1 9 12
    12 3 0 12 13 2
    
    Expected output
    5
    
  2. Example 2

    Input
    10 1
    1 1 0 1
    
    Expected output
    1
    
  3. Example 3

    Input
    10 1
    1 0 1 3
    
    Expected output
    1