n=int(input())
p=list(map(int,input().split()))
for i in range(n*n):p[i]-=1
b=[n*[1]for _ in range(n)]
def solve(p):
  i,j=divmod(p,n)
  ans=[n*[-1]for _ in range(n)]
  ans[i][j]=0
  Q=[[]for _ in range(n*n)]
  Q[0]=[(i,j)]
  dij=((0,1),(0,-1),(1,0),(-1,0))
  for i in range(n*n):
    for i,j in Q[i]:
      for di,dj in dij:
        if i+di in [-1,n] or j+dj in [-1,n]:continue
        if ans[i+di][j+dj]!=-1:continue
        if b[i+di][j+dj]!=0:
          Q[i+1].append((i+di,j+dj))
          ans[i+di][j+dj]=ans[i][j]+1
        else:
          Q[i].append((i+di,j+dj))
          ans[i+di][j+dj]=ans[i][j]
  anss=n*n
  for i in range(n):
    if ans[0][i]!=-1:anss=min(anss,ans[0][i])
    if ans[-1][i]!=-1:anss=min(anss,ans[-1][i])
    if ans[i][0]!=-1:anss=min(anss,ans[i][0])
    if ans[i][-1]!=-1:anss=min(anss,ans[i][-1])
  return anss
ans=0
for i in p:
  ans+=solve(i)
  ii,jj=divmod(i,n)
  b[ii][jj]=0
print(ans)