Young ruler Mirko has declared himself king of dwarves. Upon hearing this, Slavko felt threatened and soon declared himself king of elves! As there cannot be more than one king in the land, they have decided to resolve the issue of power once and for all.
Slavko will, along with
Mirko has, upon entering the castle, given a number
They have decided to solve the problem in the following way:
- Slavko will send his elves to the hall one by one, in the order he chooses. The next elf can enter the hall only after the one before him found a place to sit.
- The elf labeled
will first approach the dwarf labeled . If there isn't an elf sitting beside the dwarf, he will sit there. Otherwise, he will continue walking, from dwarf to dwarf, clockwise, until he finds an unclaimed dwarf.
Now the
Slavko is well prepared for this event. He has studied all the fighters and determined the strength of each one. Now he wants to send the elves to the hall in the order which, after they all sit down, will bring the most victories for him.
Help him and calculate the highest number of victories in duels that can be achieved by elves!
Input Specification
The first line of input contains the integer
The second line of input contains
The third line of input contains
The fourth line of input contains
All strengths from the input will be mutually distinct.
Output Specification
The first and only line of output must contain the maximum number of victories that can be achieved by elves.
Scoring
In test cases worth
Sample Input 1
3
2 3 3
4 1 10
2 7 3
Sample Output 1
2
Explanation for Sample Output 1
Slavko can sort the elves in the following way:
Sample Input 2
4
3 1 3 3
5 8 7 10
4 1 2 6
Sample Output 2
1
Sample Input 3
3
1 2 3
8 4 3
9 2 6
Sample Output 3
2
Comments