Task
RANDy is back at it again, but now he is ready to showcase his elite dog training skills. He's been training his dog Pompieru for many years, and now he is ready to participate in a dog trick competition.
There are tricks which can be performed in this competition, numbered from to .
Pompieru does not know how to perform the trick , so he can only perform the tricks from to .
Moreover, performing specific tricks one after the other is not always possible: he can only perform trick right after trick if he has been trained to do so.
RANDy trained Pompieru to perform pairs of consecutive tricks , meaning that Pompieru knows how to perform trick right after trick .
The participants are required to perform tricks in this specific order.
For each successfully performed trick the dog gets point.
A dog may decide to skip a trick and continue with the next trick instead.
The dogs are allowed to skip any number of tricks, even if they know how to do the tricks, but if a dog skips two consecutive tricks it gets disqualified, scoring zero points.
Help RANDy and Pompieru find the maximum number of points they can score in this competition.
Input data
The input file consists of:
- a line containing integers and .
- a line containing the integers .
- a line containing integer .
- lines, the -th of which consisting of integers , .
Output data
The output file must contain a single line consisting of integer .
Constraints and clarifications
- .
- .
- .
- for each .
- for each .
- All the pairs are distinct.
# | Points | Constraints |
---|---|---|
1 | 0 | Examples. |
2 | 15 | |
3 | 19 | |
4 | 34 | |
5 | 32 | No additional limitations. |
Example 1
stdin
6 3
4 1 1 2 2 2
2
2 2
1 2
stdout
4
Explanation
In the first sample case, Pompieru can skip the first trick, perform the second trick, skip the third and then perform the last three tricks, scoring points total.
Example 2
stdin
4 3
1 4 2 3
1
1 3
stdout
0
Explanation
In the second sample case, Pompieru can perform the first trick but does not know how to perform the second trick, so he has to skip it.
He can perform the third trick, but was not trained to perform it after the first trick, so he has to skip it again. Since he skipped two tricks in a row, he gets disqualified and scores points.