N = int(input())
Ps = list(map(lambda x: int(x), input().split()))

inf = 10**10
from collections import deque
import heapq

dirc = ((0, -1), (1, 0), (0, 1), (-1, 0))
def djikstra(s):
    checked = [[0]*(N+2) for _ in range(N+2)]
    queue = [(0, s)]
    heapq.heapify(queue)
    while queue:
        c, (y, x) = heapq.heappop(queue)
        if checked[y][x]:
            continue
        checked[y][x] = 1
        if y<1 or y>=N+1 or x<1 or x>=N+1:
            return c//inf
        for dy, dx in dirc:
            ny, nx = y+dy, x+dx
            if not checked[ny][nx]:
                heapq.heappush(queue, (c+cost[ny][nx], (ny, nx)))
        
cost = [[0]+[inf]*N+[0] if i!=0 and i!=N+1 else [0]*(N+2) for i in range(N+2)]
ans = 0
near = [[-1]*(N+2) for _ in range(N+2)]
for i in range(N**2):
    p = Ps[i]-1
    y = p//N+1
    x = p%N+1
    dji = 1
    for dy, dx in dirc:
        ny, nx = y+dy, x+dx
        if near[ny][nx]!=0:
            dji = near[ny][nx]
            break
    if dji:
        dji = djikstra((y, x))
    near[y][x] = dji
    ans += dji
    cost[y][x] = 0
print(ans)