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 solve(n, ppp):
    n2 = n + 2
    row_base = [0] * n
    for i in range(n):
        row_base[i] = min(i, n - 1 - i)

    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, row_base[j])
            is_seated[ij] = True

    ans = 0
    MOVE = (-n2, -1, 1, n2)

    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)]
        updated = set()

        while q:
            val, j = heappop(q)
            if j in updated:
                continue
            updated.add(j)
            seats[j] = val
            if is_seated[j]:
                val += 1
            for dj in MOVE:
                nj = j + dj
                if seats[nj] > val and nj not in updated:
                    heappush(q, (val, nj))
        # print(p, i, res)
        # pg(seats)

    print(ans)


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