n = int(input())
p = list(map(int,input().split()))
g = [[0]*n for i in range(n)]
a = [[1]*n for i in range(n)]
ans = 0
opt = [(1,0),(0,1),(-1,0),(0,-1)]
for i in range(1,n-1):
    for j in range(1,n-1):
        g[i][j] = min(i,j,n-i-1,n-j-1)
for i in p:
    y = (i-1)//n
    x = (i-1)%n
    ans += g[y][x]
    dq = ([(y,x)])
    a[y][x] = 0
    while dq:
        y,x = dq.pop()
        t = a[y][x]
        for j in opt:
            y1,x1 = y+j[0],x+j[1]
            if y1<0 or x1<0 or y1>=n or x1>=n:
                continue
            if g[y1][x1] > g[y][x]:
                g[y1][x1] = g[y][x]+t
                dq.append((y1,x1))

print(ans)

