import sys
input = sys.stdin.readline
n = int(input())
l = list(map(int,input().split()))
l = [((i-1)//n, (i-1) % n) for i in l]
check = [[1]*n for i in range(n)]
d =  [[min(i, n-i-1, j, n-j-1) for j in range(n)] for i in range(n)]

ans = 0
for x,y in l:
    check[x][y] = 0
    ans += d[x][y]
    q = [(x,y,d[x][y])]
    while q:
        bx,by,z = q.pop()

        if bx != 0:
            if d[bx-1][by] > z:
                d[bx-1][by] = z
                q.append((bx-1,by,z+check[bx-1][by]))
        if bx != n-1:
            if d[bx+1][by] > z:
                d[bx+1][by] = z
                q.append((bx+1,by,z+check[bx+1][by]))
        if by != 0:
            if d[bx][by-1] > z:
                d[bx][by-1] = z
                q.append((bx,by-1,z+check[bx][by-1]))
        if by != n-1:
            if d[bx][by+1] > z:
                d[bx][by+1] = z
                q.append((bx,by+1,z+check[bx][by+1]))  
print(ans)

