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

from numba import njit


# def pg(aaa):
#     for i in range(0, n2 ** 2, n2):
#         print(aaa[i:i + n2])
#     print()


@njit
def bfs(n2, s, seats, is_seated):
    q = [s]

    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))


@njit
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
        bfs(n2, (res, i), seats, is_seated)

    print(ans)


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

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