import sys
input = sys.stdin.readline
import bisect
from collections import deque


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


def print_map(N, Q):
    for i in range(N):
        print(Q[i*N:(i+1)*N])


def solve(N, P, INF=10**9):
    P = [p-1 for p in P]
    D = [N, -N, 1, -1]
    # Q[p]: 行動の最初にそのマスを踏んだ場合の、脱出までの最短コスト
    Q = [0 for _ in P]
    # LEFT[p]: 取り除いたらTrue
    LEFT = [False for _ in P]
    NN = N * N
    ans = 0

    for i in range(N):
        for j in range(N):
            k = i * N + j
            Q[k] = min(i, j, N-i-1, N-j-1) + 1
    
    for p in P:
        q = deque()
        r = INF
        for dp in D:
            np = p + dp
            if 0 <= np < NN and abs(np%N - p%N) < N-1:
                r = min(r, Q[np])
            else:
                r = 0
        
        ans += r

        LEFT[p] = True
        Q[p] = r
        q.append(p)
        while q:
            pp = q.popleft()
            for dp in D:
                np = pp + dp
                if 0 <= np < NN and abs(np%N - pp%N) < N-1:
                    # 4隣接が除去済みマスなら、コストなしで到達可能
                    if LEFT[np] and Q[np] > Q[pp]:
                        Q[np] = Q[pp]
                        q.append(np)
                    # elif not LEFT[np] and Q[np] > Q[pp] + 1:
                    #    Q[np] = Q[pp] + 1
                    #    q.append(np)
    return ans


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