#P6117. Coin Repositioning on a Giant Table

    ID: 19339 Type: Default 1000ms 256MiB

Coin Repositioning on a Giant Table

Coin Repositioning on a Giant Table

JOI has a huge collection of rare coins spread over a gigantic table. The table can be viewed as a grid with dimensions \((2\times10^9+1)\times (2\times10^9+1)\). Initially, he has \(2N\) coins, where the \(i\)th coin (\(1\le i\le 2N\)) is placed on the cell at coordinates \((X_i, Y_i)\).

His goal is to rearrange the coins so that every cell \((x,y)\) with \(1\le x\le N\) and \(1\le y\le 2\) contains exactly one coin. In order not to damage the coins, the only allowed operation is to choose one coin and move it to an adjacent cell (two cells are adjacent if and only if they share a common edge). Coins are allowed to temporarily occupy the same cell during the process.

Compute the minimum number of moves required to achieve the target configuration.

Note: The Manhattan distance is used for the cost of each move. For any coin at position \((x,y)\) moved to \((x',y')\), the cost is \(|x-x'|+|y-y'|\).

Hint:
Since the Manhattan distance is separable, you can independently consider the moves in the x-direction and the moves in the y-direction. In the final configuration, the x-coordinates of the coins must be exactly \(1,1,2,2,\ldots, N,N\). For the y-coordinates, exactly \(N\) coins must go to row 1 and \(N\) to row 2.

inputFormat

The input is given as follows:

N
X1 Y1
X2 Y2
... 
X2N Y2N

Here, \(N\) is a positive integer, and each of the next \(2N\) lines contains two integers \(X_i\) and \(Y_i\) representing the initial position of the \(i\)th coin.

outputFormat

Print a single integer representing the minimum number of moves needed to achieve the target configuration.

sample

1
1 1
2 100
99