Moving Paintings

Time limit: 0.4s Memory limit: 64MB Input: Output:

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 ABA \cdot B rectangle, which can be rotated at will.

Giorgio wants the sequence of paintings to respect a simple height property: for a given height value HH, decided by Giorgio, the sequence should be made of two concatenated sequences:

  • The first part of the sequence only has paintings shorter than HH.
  • The second part of the sequence only has paintings taller than HH.

Notice that Giorgio will choose an H value which is different from every rectangle’s AA and BB dimensions. To realize the height property, Giorgio will start by rotating the paintings (thus exchanging the AA and BB 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 HH. The second line contains integer NN. The next NN lines contain two integers each: the dimensions AiA_i and BiB_i of the ii-th painting.

Output data

You need to write a single line with an integer: the minimum number of swaps.

Constraints and clarifications

  • 1H1 000 000 0001 \leq H \leq 1 \ 000 \ 000 \ 000.
  • 1N1 000 0001 \leq N \leq 1 \ 000 \ 000.
  • 1Ai,Bi1 000 000 0001 \leq A_i, B_i \leq 1 \ 000 \ 000 \ 000 for each i=0N1i = 0 \dots N − 1.
  • AiHA_i \neq H and BiHB_i \neq H for each i=0N1i = 0 \dots N - 1.
# Score Constraints
1 5 Examples
2 25 Ai=BiA_i = B_i for all ii (all paintings are squares).
3 25 N10N \leq 10
4 25 N1 000N \leq 1 \ 000
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 HH 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 (4154 \cdot 15 becomes 15415 \cdot 4). 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 33 paintings being shorter than HH, the last 22 being taller.

Log in or sign up to be able to send submissions!