import java.util.Scanner;

public class D {
    public static int distMoved = 0;
    public static int pawnCount = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        pawnCount = scanner.nextInt();
        boolean[][] board = new boolean[pawnCount][pawnCount];
        scanner.nextLine();

        for (int i = 0; i < pawnCount; i++) {
            int x = scanner.nextInt() - 1;
            int y = scanner.nextInt() - 1;
            scanner.nextLine();

            board[x][y] = true;
        }
        scanner.close();
        scanner = null;
        System.gc();

        solveRows(board);
        solveCols(board);

        board = null;
        System.gc();
        
        System.out.println(distMoved);
    }

    public static int getPawnsInRow(boolean[][] board, int row) {
        int pawns = 0;

        for (int col = 0; col < pawnCount; col++) {
            pawns += board[row][col] ? 1 : 0;
        }

        return pawns;
    }

    public static int getPawnsInCol(boolean[][] board, int col) {
        int pawns = 0;

        for (int row = 0; row < pawnCount; row++) {
            pawns += board[row][col] ? 1 : 0;
        }

        return pawns;
    }

    public static void solveRows(boolean[][] board) {
        for (int i = 0; i < pawnCount; i++) {
            if (getPawnsInRow(board, i) != 0) continue;

            movePawn: for (int row = 0; row < pawnCount; row++) {
                if (row == i) continue;

                // Moving the first pawn in the row only if there are two pawns in the row
                int firstPawn = -1;
                for (int col = 0; col < pawnCount; col++) {
                    if (!board[row][col]) continue;

                    if (firstPawn == -1) {
                        firstPawn = col;
                        continue;
                    }

                    board[i][col] = true;
                    board[row][col] = false;
                    distMoved += Math.abs(i - row);
                    break movePawn;
                }
            }
        }
    }

    public static void solveCols(boolean[][] board) {
        final int len = board.length;
        for (int j = 0; j < len; j++) {
            if (getPawnsInCol(board, j) != 0) continue;
            
            movePawn: for (int col = 0; col < len; col++) {
                if (col == j) continue;

                // Moving the first pawn in the row only if there are two pawns in the row
                int firstPawn = -1;
                for (int row = 0; row < len; row++) {
                    if (!board[row][col]) continue;

                    if (firstPawn == -1) {
                        firstPawn = row;
                        continue;
                    }

                    board[row][j] = true;
                    board[row][col] = false;
                    distMoved += Math.abs(j - col);
                    break movePawn;
                }
            }
        }
    }
}