<?php

class AtCoderStream
{
    public function readLine($format)
    {
        return fscanf(STDIN, $format);
    }

    public function writeLine($format)
    {
        echo $format, "\n";
    }
}

// export
$stream = new AtCoderStream();

[$N] = $stream->readLine('%d');

$seats = [];
$hates = [];
$arr1  = [];
$arr2  = [];
for ($y = 0; $y < $N; ++$y) {
    $arr1[] = 1;
    $arr2[] = $N;
}
for ($x = 0; $x < $N; ++$x) {
    $seats[] = $arr1;
    $hates[] = $arr2;
}

$hateCount = 0;
for ($Ni = 0, $Nj = $N * $N; $Ni < $Nj; ++$Ni) {
    [$P] = $stream->readLine('%d');
    $Px  = ($P - 1) % $N;
    $Py  = ($P - 1 - $Px) / $N;

    $seats[$Px][$Py] = 0;
    $newHates        = $hates;
    $searchMap       = $hates;

    $newHates[$Px][$Py]  = 0;
    $searchMap[$Px][$Py] = 0;
    $minX                = $Px;
    $minY                = $Py;

    do {
        if (($minX == 0 || $minX == $N - 1 || $minY == 0 || $minY == $N - 1)
            && $newHates[$minX][$minY] == 0) {
            continue 2;
        }
        $searchMap[$minX][$minY] = -1;
	    findMinHateCount($newHates, $searchMap, $seats, $N, $minX, $minY);

        $minSearch = $N;
        for ($x = 0; $x < $N; ++$x) {
            for ($y = 0; $y < $N; ++$y) {
                $search = $searchMap[$x][$y];
                if ($search > -1 && $search < $minSearch) {
                    $minSearch = $search;
                    $minX      = $x;
                    $minY      = $y;
                }
            }
        }
    } while ($minSearch < $N);

    $minHate = $N;
    for ($i = 0, $j = $N - 1; $i < $j; ++$i) {
        $hate = $newHates[$i][0];
        if ($hate == 0) {
            continue 2;
        } elseif ($hate < $minHate) {
            $minHate = $hate;
        }

        $hate = $newHates[$j][$i];
        if ($hate == 0) {
            continue 2;
        } elseif ($hate < $minHate) {
            $minHate = $hate;
        }

        $hate = $newHates[$j - $i][$j];
        if ($hate == 0) {
            continue 2;
        } elseif ($hate < $minHate) {
            $minHate = $hate;
        }

        $hate = $newHates[0][$j - $i];
        if ($hate == 0) {
            continue 2;
        } elseif ($hate < $minHate) {
            $minHate = $hate;
        }
    }
    $hateCount += $minHate;
}

$stream->writeLine($hateCount);

function findMinHateCount(array &$hates, array &$searchMap, array $seats, int $N, int $x, int $y)
{
    $hate = $hates[$x][$y];

    $newHate = $hate + $seats[$x - 1][$y];
    if ($x - 1 >= 0 && $newHate < $hates[$x - 1][$y]) {
        $hates[$x - 1][$y]     = $newHate;
        $searchMap[$x - 1][$y] = $newHate;
    }

    $newHate = $hate + $seats[$x][$y - 1];
    if ($y - 1 >= 0 && $newHate < $hates[$x][$y - 1]) {
        $hates[$x][$y - 1]     = $newHate;
        $searchMap[$x][$y - 1] = $newHate;
    }

    $newHate = $hate + $seats[$x + 1][$y];
    if ($x + 1 < $N && $newHate < $hates[$x + 1][$y]) {
        $hates[$x + 1][$y]     = $newHate;
        $searchMap[$x + 1][$y] = $newHate;
    }

    if ($y + 1 < $N && $newHate < $hates[$x][$y + 1]) {
        $hates[$x][$y + 1]     = $newHate;
        $searchMap[$x][$y + 1] = $newHate;
    }
}
