from collections import deque

n = int(input())
perm = [int(x) for x in input().split()]

arr = []
for x in range(1,n**2+1,n):
    arr.append([y for y in range(x,x+n)])
ans = 0

DX = [-1,0,1,0]
DY = [0,-1,0,1]

exits = set()
for x in range(n):
    exits.add((0,x))
    exits.add((n-1,x))
for x in range(n):
    exits.add((x,0))
    exits.add((x,n-1))

def counts(item):
    posx = (item-1)//n
    posy = ((item-1) % n)
    q = deque()
    q.append([posx,posy,0])
    visited = [[0 for i in range(n)] for j in range(n)]
    visited[posx][posy] = 1
    cost = []
    while len(q):
        x,y,cst = q.popleft()
        if (x,y) in exits:
            cost.append(cst)
            continue
        for dx,dy in zip(DX,DY):
            newX,newY = dx + x, dy + y
            if (0 <= newX < n) and (0 <= newY < n) and not visited[newX][newY]:
                visited[newX][newY] = 1
                if arr[newX][newY]:
                    q.append([newX,newY,cst+1])
                else:
                    q.append([newX,newY,cst])
    arr[posx][posy] = 0
    # print(arr,min(cost))
    return min(cost)

for item in perm:
    ans+=counts(item)
print(ans)