from collections import deque
d=deque([1, 2, 3])
d.append(4)      # [1, 2, 3, 4]
x=d.pop()        # x=4, [1, 2, 3]

def f(x, y):
  d=deque([[x, y]])
  sumi=[[-1]*(N+2)]+[[-1]+[1]*N+[-1] for _ in range(N)]+[[-1]*(N+2)]
  cand=kazu[x][y]
  while d:
    data=d.popleft()
    cand=min(cand, kazu[data[0]][data[1]])
    sumi[data[0]][data[1]]=0
    if sumi[data[0]-1][data[1]]==1 and hito[data[0]-1][data[1]]==0:
      d.append([data[0]-1, data[1]])
    if sumi[data[0]+1][data[1]]==1 and hito[data[0]+1][data[1]]==0:
      d.append([data[0]+1, data[1]])
    if sumi[data[0]][data[1]-1]==1 and hito[data[0]][data[1]-1]==0:
      d.append([data[0], data[1]-1])
    if sumi[data[0]][data[1]+1]==1 and hito[data[0]][data[1]+1]==0:
      d.append([data[0], data[1]+1])
  return cand

N=int(input())
P=list(map(int, input().split()))

hito=[[-1]*(N+2)]+[[-1]+[1]*N+[-1] for _ in range(N)]+[[-1]*(N+2)]

kazu=[[0]*(N+2) for _ in range(N+2)]
for i in range(1, N+1):
  for j in range(1, N+1):
    kazu[i][j]=min(i-1, j-1, N-i, N-j)

ans=0
for p in P:
  x=(p-1)%N+1
  y=(p-1)//N+1
  ans+=f(x, y)
  hito[x][y]=0
  
print(ans)