## APIO '07 P3 - Zoo

View as PDF

Points: 20 (partial)
Time limit: 2.0s
Memory limit: 16M

Problem type

The pride of the Asia-Pacific region is the newly constructed Great Circular Zoo. Situated on a small Pacific island, it consists of a large circle of different enclosures, each containing its own exotic animal as illustrated below.

You are in charge of public relations for the zoo, which means it is your job to keep people as happy as possible. A busload of schoolchildren has just arrived, and you are eager to please them. However, this is no easy task—there are animals that some children love, and there are animals that some children fear. For example, little Alex loves monkeys and koalas because they are cute, but fears lions because of their sharp teeth. On the other hand, Polly loves lions because of their beautiful manes, but fears koalas because they are extremely smelly.

You have the option of removing some animals from their enclosures, so that children are not afraid. However, you are worried that if you remove too many animals then this will leave the children with nothing to look at. Your task is to decide which animals to remove so that as many children can be made happy as possible.

Each child is standing outside the circle, where they can see five consecutive enclosures. You have obtained a list of which animals each child fears, and which animals each child loves. A child will be made happy if either:

• At least one animal they fear is removed from their field of vision, or:
• At least one animal they love is not removed from their field of vision.

For example, consider the list of children and animals illustrated below:

Child Enclosures Visible Fears Loves
Alex 2, 3, 4, 5, 6 Enclosure 4 Enclosures 2, 6
Polly 3, 4, 5, 6, 7 Enclosure 6 Enclosure 4
Chaitanya 6, 7, 8, 9, 10 Enclosure 9 Enclosures 6, 8
Hwan 8, 9, 10, 11, 12 Enclosure 9 Enclosure 12
Ka-Shu 12, 13, 14, 1, 2 Enclosures 12, 13, 2

Suppose you remove the animals from enclosures and . This will make Alex and Ka-Shu happy, because at least one animal that they fear has gone. This will also keep Chaitanya happy, since both enclosures and still contain animals that he loves. However, both Polly and Hwan will be unhappy, since they cannot see any animals that they love but they can still see all the animals that they fear. This arrangement therefore gives a total of three happy children. Now suppose you put these animals back into their enclosures, and remove the animals from enclosures and instead. Alex and Polly will be happy because the animals that they fear in enclosures and have gone. Chaitanya will be happy because, even though animal has gone, he can still see the animal in enclosure which he loves. Likewise, Hwan will be happy because she can now see the animal in enclosure , which she loves. The only person unhappy will be Ka-Shu.

Finally, suppose you put the animals back once more and then remove only the animal from enclosure . Ka-Shu will now be happy since one animal that he fears has been removed, and Alex, Polly, Chaitanya and Hwan will all be happy since they can all see at least one animal that they love. Thus this arrangement gives five happy children, the largest number possible.

#### Input

The first line of input will be of the form , where is the number of animal enclosures and is the number of children . The enclosures are numbered clockwise around the circle.

Following this will be additional lines of input, where each line describes a single child. Each of these lines will be of the form

where:

• is the first enclosure that the child can see . In other words, the child can see enclosures , , , and . Note that numbers larger than wrap back around the circle, so if and then the child can see enclosures , , , and .
• is the number of animals that the child fears, and is the number of animals that the child loves.
• Enclosures contain the animals that the child fears .
• Enclosures contain the animals that the child loves .
• No two of the integers are equal, and all of these integers describe enclosures that the child can see.

Children will be listed in sorted order according to the first enclosure (so the child with lowest will appear first and the child with largest will appear last). Note that more than one child may have the same first enclosure .

#### Output

Output must consist of a single integer, giving the largest number of children that can be made happy.

#### Sample Input 1

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

#### Sample Output 1

5

#### Sample Input 2

12 7
1 1 1 1 5
5 1 1 5 7
5 0 3 5 7 9
7 1 1 7 9
9 1 1 9 11
9 3 0 9 11 1
11 1 1 11 1

#### Sample Output 2

6

#### Explanation

The first sample case is the example discussed earlier, in which all children can be made happy.

The second sample case is an example in which it is impossible to make all children happy.

#### Scoring

The score for each input scenario will be if the correct answer is written to the output file, and otherwise.