import sys
from heapq import heappop, heappush
import numpy as np

from numba import njit, int64


@njit(int64(int64, int64[:]), cache=True)
def solve(n, ppp):
    n2 = n + 2
    seats = [-1] * (n2 * n2)
    is_seated = [False] * (n2 * n2)
    for i in range(n):
        ud = min(i, n - 1 - i)
        for j in range(n):
            ij = (i + 1) * n2 + j + 1
            seats[ij] = min(ud, j, n - 1 - j)
            is_seated[ij] = True

    ans = 0

    for p in ppp:
        i, j = divmod(p - 1, n)
        i = (i + 1) * n2 + j + 1
        res = seats[i]
        ans += res
        is_seated[i] = False

        q = [(res, i)]
        while q:
            val, j = heappop(q)
            seats[j] = val
            if is_seated[j]:
                val += 1

            nj = j - n2
            if seats[nj] > val:
                heappush(q, (val, nj))
            nj = j - 1
            if seats[nj] > val:
                heappush(q, (val, nj))
            nj = j + 1
            if seats[nj] > val:
                heappush(q, (val, nj))
            nj = j + n2
            if seats[nj] > val:
                heappush(q, (val, nj))

    return ans


n = int(sys.stdin.buffer.readline())
ppp = np.array(list(map(int, sys.stdin.buffer.read().split())), dtype=np.int64)
print(solve(n, ppp))

# solve.inspect_types()
# bfs.inspect_types()
