import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
p = list(map(int, input().split()))

d = [[None]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        d[i][j] = min(i, n-1-i, j, n-1-j)
vacant = [[False]*n for _ in range(n)]
from numba import njit
@njit
def sub(x,y):
    v = d[x][y]
    q = [(v, x, y)]
    dx = [0,1,0,-1]
    dy = [1,0,-1,0]
    while q:
        v,i,j = q.pop()
        for xx, yy in zip(dx,dy):
            ii = i+xx
            jj = j+yy
            if ii<0 or jj<0 or ii>=n or jj>=n:
                continue
            if d[ii][jj]>v:
                d[ii][jj] = v
                nv = v+1 if not vacant[ii][jj] else v
                q.append((nv, ii, jj))
ans = 0
for v in p:
    v -= 1
    x,y = v//n, v%n
    ans += d[x][y]
#     print(v, d[x][y], d)
    sub(x,y)
    vacant[x][y] = True
print(ans)