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


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


def sign(x):
    return (x > 0) - (x < 0)



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]
    q = deque()
    cache = set()
    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
    
    x = 0
    for p in P:
        q.clear()
        cache.clear()
        r = INF
        for dp in D:
            np = p + dp
            if 0 <= np < NN and abs(np%N - p%N) < N-1:
                r = min(r, abs(Q[np]))
            else:
                r = 0
        
        ans += r
        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 and np not in cache:
                    qnp, qpp = Q[np], Q[pp]
                    # 4隣接が除去済みマスなら、コストなしで到達可能
                    if qnp < 0 and abs(qnp) > abs(qpp):
                        Q[np] = abs(qpp) * sign(qnp)
                        q.append(np)
                        cache.add(np)
                    elif qnp > 0 and abs(qnp) > abs(qpp) + 1:
                        Q[np] = (abs(qpp) + 1) * sign(qnp)
                        q.append(np)
                        cache.add(np)
    return ans


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