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 prepare_seats(n):
    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
    return seats, is_seated


@njit
def bfs(n2, s, seats, is_seated):
    MOVE = (-n2, -1, 1, n2)

    q = [s]
    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))


@njit
def solve(n, ppp):
    n2 = n + 2
    seats, is_seated = prepare_seats(n)

    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.int32)
solve(n, ppp)
