N=int(input())
P=list(map(int,input().split()))
sx, sy, gx, gy = 0,0,0,0
g=0
town = []
edge = [2] * (N+2) # 上下を塀で囲む
for i in range(N):
    line = [1]*(N)
    line = [2] + line + [2] # 左右を塀で囲む
    town.append(line)
town = [edge] + town + [edge] #この問題でいうところの街の地図
#=== 深さ優先探索　開始 ===#
def dfs(sx,sy,g):
  if visited[sx][sy]==True:
    return
  visited[sx][sy] = True # スタート地点を"訪問済み"にする
  x, y, z = sx,sy,g
  if town[x][y] == 2:
    count.append(z)
    return
  z+=town[x][y]
  dfs(x-1,y,z)
  dfs(x+1,y,z)
  dfs(x,y+1,z)
  dfs(x,y-1,z)
sss=0
for i in range(N*N):
  sx=P[i]%N
  if sx==0:
    sx=N
  sy=int((P[i]-1)//N)+1
  visited = [[False]*(N+2) for _ in range(N+2)] #街のどこに訪れたかを記録する
  count=[]
  town[sx][sy]=0
  dfs(sx,sy,0)
  sss+=(min(count))
print(sss)