cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))

from collections import deque
q = deque()

space = [1]*(n*n)

dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]

ans = 0
for pi in p:
    row, col = divmod(pi-1, n)

    q.clear()
    q.append((row, col))

    dist = [-1]*(n*n)
    dist[row*n+col] = 0

    ret = list()
    while q:
        ph, pw = q.popleft()
        for dyi, dxi in zip(dy,dx):
            if 0<=ph+dyi<n and 0<=pw+dxi<n:
                if dist[(ph+dyi)*n+(pw+dxi)] == -1:
                    q.append((ph+dyi,pw+dxi))
                    dist[(ph+dyi)*n+(pw+dxi)] = dist[ph*n+pw]+int(space[(ph+dyi)*n+(pw+dxi)]==1)
            else:
                ret.append(dist[ph*n+pw])
    ans += min(ret)
    space[row*n + col] = 0

print(ans)