import sys
input = sys.stdin.readline
from numba import njit
import numpy


def read():
    N = int(input().strip())
    P = list(map(int, input().strip().split()))
    return N, P


@njit
def init_Q(N, Q):
    for i in range(N):
        for j in range(N):
            k = i * N + j
            Q[k, 0] = min([i, j, N-i-1, N-j-1]) + 1
            Q[k, 1] = 1   


@njit
def dfs(N, Q, visited, p):
    visited[p] = True
    D = [N, -N, 1, -1]
    for dp in D:
        np = p + dp
        if 0 <= np < N*N and abs(np%N - p%N) < N-1:
            # 4隣接が除去済みマスなら、コストなしで到達可能
            if not visited[np] and Q[np][0] > Q[p][0] + Q[np][1]:
                Q[np][0] = Q[p][0] + Q[np][1]
                dfs(N, Q, visited, p)


@njit
def np_solve(N, P, INF=10**9):
    Q = numpy.empty((N*N, 2), dtype=numpy.int32)
    init_Q(N, Q)
    D = [N, -N, 1, -1]
    visited = numpy.zeros(N*N, dtype=numpy.bool_)
    ans = 0

    for p in P:
        r = INF
        for dp in D:
            np = p + dp
            if 0 <= np < N*N and abs(np%N - p%N) < N-1:
                r = min(r, abs(Q[np][0]))
            else:
                r = 0
        ans += r
        Q[p][0] = r
        Q[p][1] = 0
        visited[:] = False
        dfs(N, Q, visited, p)
    return ans


def solve(N, P):
    # Q[p][0]: 行動の最初にそのマスを踏んだ場合の、脱出までの最短コスト
    # Q[p][1]: 退室済みなら0
    P = numpy.array(P, dtype=numpy.int) - 1
    return np_solve(N, P)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))
