from numba import njit, jit, int32
import numpy as np
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()))
p = np.array(input().split(), dtype=np.int32)


@njit('(i4,i4[::1])', cache=True)
def main(n, p):
#     d = [[None]*n for _ in range(n)]
    d = np.zeros((n,n), dtype=np.int32)
    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)]
    vacant = np.zeros((n,n), dtype=np.int32)

    ans = 0
    for v in p:
        v -= 1
        x,y = v//n, v%n
        ans += d[x,y]
    #     print(v, d[x][y], d)
        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))

        vacant[x,y] = 1
    return ans
ans = main(n,p)
print(ans)