import sys
from collections import deque

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    P = list(map(int,input().split()))

    DP = [[0]*N for i in range(N)]

    for i in range(N):
        for j in range(N):
            DP[i][j] = min(i,N-i-1,j,N-j-1)

    def shoot(i,j,s,S):
        if not j*N+i+1 in S:
            S.add((i,j))
            if DP[i][j]>s:
                DP[i][j] = s
                if i>0:
                    shoot(i-1,j,s+1,S)
                if i<N-1:
                    shoot(i+1,j,s+1,S)
                if j>0:
                    shoot(i,j-1,s+1,S)
                if j<N-1:
                    shoot(i,j+1,s+1,S)
            elif DP[i][j]==-1:
                if i>0:
                    shoot(i-1,j,s,S)
                if i<N-1:
                    shoot(i+1,j,s,S)
                if j>0:
                    shoot(i,j-1,s,S)
                if j<N-1:
                    shoot(i,j+1,s,S)

            

    ans = 0
    for p in P:
        i = (p-1)%N
        j = (p-1)//N
        ans += DP[i][j]
        S = set()
        Q = deque([((i,j),DP[i][j])])
        flag = True
        while len(Q)>0:
            A,s = Q.pop()
            a,b = A
            if flag:
                flag=False
                S.add(b*N+a+1)
                if a>0:
                    Q.append(((a-1,b),s))
                if a<N-1:
                    Q.append(((a+1,b),s))
                if b>0:
                    Q.append(((a,b-1),s))
                if b<N-1:
                    Q.append(((a,b+1),s))
            elif not b*N+a+1 in S:
                S.add(b*N+a+1)
                if DP[a][b]>s:
                    DP[a][b]=s
                    if a>0:
                        Q.append(((a-1,b),s+1))
                    if a<N-1:
                        Q.append(((a+1,b),s+1))
                    if b>0:
                        Q.append(((a,b-1),s+1))
                    if b<N-1:
                        Q.append(((a,b+1),s+1))
                if DP[a][b]==-1:
                    if a>0:
                        Q.append(((a-1,b),s))
                    if a<N-1:
                        Q.append(((a+1,b),s))
                    if b>0:
                        Q.append(((a,b-1),s))
                    if b<N-1:
                        Q.append(((a,b+1),s))
        DP[i][j] = -1
        #for i in range(N):
        #    print(*DP[i])
        #print()
    print(ans)

if __name__ == '__main__':
    main()

