import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    N=I()
    P=LI()
    d=[[1]*N for _ in range(N)]
    
    def ij_to(i,j):
        return i*N+j
    
    def num_to(X):
        return (X//N,X%N)
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    from collections import deque
    ans=0
    
    for pi in range(N*N):
        t=P[pi]-1
        i,j=num_to(t)
        d[i][j]=0
        dq=deque()#コストと位置
        dq.append((0,i,j))
        use=[[0]*N for _ in range(N)]
        use[i][j]=1
        while True:
            cost,ii,jj=dq.pop()
            #print(pi,cost,ii,jj)
            if ii==0 or ii==(N-1) or jj==0 or jj==(N-1):
                ans+=cost
                break
            
            for k in range(4):
                ni=ii+dx[k]
                nj=jj+dy[k]
                if use[ni][nj]==0:
                    use[ni][nj]=1
                    if d[ni][nj]==1:
                        dq.appendleft((cost+1,ni,nj))
                    else:
                        dq.append((cost,ni,nj))
                        
    print(ans)
                
        
        
        

main()
