In his free time, Giorgio likes to go look at paintings. He’s actually not really into fancy Louvre level stuff; in fact, he doesn’t even need the paintings to be real paintings: any sequence of rectangular frames on a wall is OK for him, as long as they are in the right order!
Today, Giorgio noticed that one of the walls in his university has a long sequence of paintings (mixed with “Best Paper Award” certificates), but he thinks that the heights are just too random, and he wants to fix that.
Each painting can be seen as an rectangle, which can be rotated at will.
Giorgio wants the sequence of paintings to respect a simple height property: for a given height value , decided by Giorgio, the sequence should be made of two concatenated sequences:
- The first part of the sequence only has paintings shorter than .
- The second part of the sequence only has paintings taller than .
Notice that Giorgio will choose an H value which is different from every rectangle’s and dimensions. To realize the height property, Giorgio will start by rotating the paintings (thus exchanging the and dimensions). Giorgio can rotate any number of paintings he wants, because he makes the rules.
After the “rotation phase”, it’s not guaranteed that the height property is respected. To solve this, Giorgio will actually move the paintings around: he will repeatedly choose two paintings and swap them. In the end, the sequence will be reordered and Giorgio will be happy.
Rotating paintings is easy, but moving them is very hard: some paintings can be quite heavy! Help Giorgio compute the minimum number of swaps needed to reorder the sequence.
Input data
The first line contains integer . The second line contains integer . The next lines contain two integers each: the dimensions and of the -th painting.
Output data
You need to write a single line with an integer: the minimum number of swaps.
Constraints and clarifications
- .
- .
- for each .
- and for each .
# | Score | Constraints |
---|---|---|
1 | 5 | Examples |
2 | 25 | for all (all paintings are squares). |
3 | 25 | |
4 | 25 | |
5 | 20 | No additional limitations. |
Example 1
stdin
10
3
20 19
2 2
17 15
stdout
1
Explanation
In the first sample case Giorgio can just swap the first two paintings. After that, the sequence will be made of two parts: the first painting being shorter than and the last two paintings being taller.
Example 2
stdin
10
5
11 13
4 15
12 14
9 7
5 8
stdout
2
Explanation
In the second sample case a possible solution for Giorgio is to rotate the second painting ( becomes ). After that he can swap the first and last paintings, and then swap the second-to-last and the third-to-last paintings. In the end, the sequence will be made of two parts: the first paintings being shorter than , the last being taller.