
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline


def ij_to_n(i,j,W):
    return i*W+j

def n_to_ij(n,W):
    return (n//W,n%W)


def n_to_ij_oneoffset(n,W):
    #n+=1
    return (1+(n-1)//W,1+(n-1)%W)


def acinput():
    return list(map(int, input().split(" ")))
       

directions=[[1,0],[0,1],[-1,0],[0,-1]]


N=int(input())
H,W=N,N


""" field = [[-1]*(N+2)]

for i in range(N):
    tmp=[-1]
    
    inputv=[]
    tmp.extend([0]*N)
    tmp.append(-1)           
    field.append(tmp)
    
for i in range(1,N+1):
    for j in range(1,N+1):
        field[i][j]=N*(i-1)+j
        
field.append([-1]*(N+2)) """

#dp=np.full([H+2,W+2],300)
#state=np.full([H+2,W+2],1)

def array(size,init):
    return [[init for j in range(size[1])] for i in range(size[0])]

dp=array([H+2,W+2],300)
state=array([H+2,W+2],1)

def propagete(x):
    global directions,state
    v=dp[x[0]][x[1]]+state[x[0]][x[1]]
    #print(x,v)
    nd=[0,0]
    for sd in directions:
        nd[0]=x[0]+sd[0]
        nd[1]=x[1]+sd[1]
        
        if dp[nd[0]][nd[1]]>v:
            dp[nd[0]][nd[1]]=v
            propagete(nd)
            

for i in range(1,N+1):
    for j in range(1,N+1):
        dp[i][j]=min(i-1,H-i,j-1,W-j)
        
P=acinput()  


tmp=[0]*(N+2)
dp[1]=tmp
dp[H]=tmp

for j in range(H+2):
    dp[j][1]=0
    dp[j][W]=0
    
    dp[j][0]=0
    dp[j][W+1]=0

dp[0]=tmp
dp[H+1]=tmp


#print(dp)
res=0
for p in P:
    r=n_to_ij_oneoffset(p,N)
    #print(p,r)
    #print(dp[r[0],r[1]])
    res+=dp[r[0]][r[1]]
    state[r[0]][r[1]]=0
    propagete(r)
    
print(res)

#print(dp)



