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
Finally, suppose you put the animals back once more and then remove only the animal from
enclosure
Input
The first line of input will be of the form
Following this will be
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
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
The second sample case is an example in which it is impossible to make all
Scoring
The score for each input scenario will be
Comments