from collections import deque
n = int(input())
p = list(map(int,input().split()))
maze = [[0]*(n+2)] + [[0]+[-1]*n+[0] for i in range(n)] + [[0]*(n+2)]
opt = [(1,0),(0,1),(-1,0),(0,-1)]
ans = 0
for k in p:
    # print(k,ans)
    lim = 250
    j = (k-1) // n +1
    i = (k-1) % n +1
    temp = [[-1]*(n+2) for i in range(n+2)]
    temp[j][i] = 0
    dq = deque([(j,i)])
    while dq:
            x,y = dq.popleft()
            for l in opt:
                x1,y1 = x+l[0],y+l[1]
                if maze[x1][y1] == -1 and temp[x1][y1] == -1:
                    temp[x1][y1] = temp[x][y] + 1
                    dq.append((x1,y1))
                if maze[x1][y1] == 1 and temp[x1][y1] == -1:
                    temp[x1][y1] = temp[x][y]
                    dq.appendleft((x1,y1))
                if maze[x1][y1] == 0:
                    ans += temp[x][y]
                    dq = False
                    maze[j][i] = 1
                    # print(x,y)
                    break
print(ans)