from collections import deque

n = int(input())
p = list(map(int, input().split()))

cost = [[0]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        cost[i][j]=min(min(i,n-i-1),min(j,n-j-1))
dx=[1,0,-1,0]
dy=[0,1,0,-1]
ans=0
for i in range(n*n):
    pi = (p[i]-1)%n
    pj = (p[i]-1)//n
    cnt=cost[pi][pj]
    ans+=cost[pi][pj]
    cost[pi][pj]=-cnt
    d=deque()
    d.append((pi,pj,cnt))
    while d:
        ni,nj,cnt=d.popleft()
        for j in range(4):
            nwi = ni+dx[j]
            nwj = nj+dy[j]
            if not(0<=nwi<n):
                continue
            if not(0<=nwj<n):
                continue
            if cost[nwi][nwj]>cnt:
                cost[nwi][nwj]=cnt
                d.append((nwi,nwj,cnt+1))
                continue
            if -cost[nwi][nwj]>cnt:
                cost[nwi][nwj]=-cnt
                d.append((nwi,nwj,cnt))
print(ans)
