from collections import deque
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
s = [[0]*n for i in range(n)]
emp = [[False]*n for i in range(n)]
for i in range(n):
    for j in range(n):
        s[i][j] = min([i,n-1-i,j,n-1-j])
sm = 0
for z in a:
    x = (z-1)//n
    y = (z-1)%n
    sm += s[x][y]
    emp[x][y] = True
    q = deque([(x,y)])
    while len(q)>0:
        i,j = q.popleft()
        p = s[i][j]+(0 if emp[i][j] else 1)
        for dx,dy in [(0,1),(0,-1),(1,0),(-1,0)]:
            u = i+dx
            v = j+dy
            if 0<=u<n and 0<=v<n and s[u][v]>p:
                s[u][v] = p
                q.append((u,v))
print(sm)
