시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 512 MB 5 0 0 0.000%

문제

A heated game of “chess-tidying” starts with a messy board containing several king pieces strewn across the cells. The player’s job is to put all of the pieces into a line along the primary diagonal of the board, which runs along black squares.

In each move, unlike in normal chess, one king may be moved one place either horizontally or vertically (but not both) to another unoccupied cell.

Given an instance of the game, find how many moves you will need in order to finish it.

Figure K.1: Illustration of Sample Input 2. In this case, the minimum number of moves necessary to put all of the kings along the black diagonal is 28 as pictured.

입력

  • One line with the number of rows and columns, n (1 ≤ n ≤ 500).
  • Each of the following n lines contains the two-dimensional integer coordinates c and r (1 ≤ c, r ≤ n), the position of one of the kings.

Each of the kings starts at a unique position.

출력

Output the minimum number of moves necessary to cover the main diagonal (r = c) with kings.

예제 입력 1

3
1 1
2 3
3 2

예제 출력 1

2

예제 입력 2

8
6 4
6 8
5 5
5 4
4 8
5 7
7 4
3 7

예제 출력 2

28