import sys,queue,math,copy,itertools,bisect,collections
from heapq import *

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**3
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    P = LI()

    path = [[0] * N for _ in range(N)]
    seat = [[1] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            path[i][j] = min(min(i,N-1-i),min(j,N-1-j))

    ans = 0
    for a in P:
        i = (a-1) % N
        j = (a-1) // N
        ans += path[i][j]

        seat[i][j] = 0

        q = collections.deque()
        q.append((i,j,path[i][j]))
        while q:
            x,y,z = q.popleft()
            z += seat[x][y]
            for dx,dy in ((0,1),(1,0),(0,-1),(-1,0)):
                if 0 <= x+dx < N and 0 <= y+dy < N:
                    if path[x+dx][y+dy] > z:
                        path[x+dx][y+dy] = z
                        q.append((x+dx,y+dy,z))
    print(ans)

if __name__ == '__main__':
    main()