from collections import deque
n = int(input())
Ps = list(map(int, input().split()))

def hw(p):
    h,w = divmod(p,n)
    return h,w

def toP(h,w):
    return h * n + w

fs = [[0]*n for i in range(n)]
## フロアフィールドを作る
for i in range(n):
    x = min(i,n-1-i)
    for j in range(n):
        y = min(j,n-1-j)
        fs[i][j] = min(x,y)

ans = 0
mvs = [(1,0),(-1,0),(0,1),(0,-1)]
# occupied = [[1]*n for i in range(n)]
occupied = [1] * (n**2)
for p in Ps:
    p -= 1
    h,w = hw(p) 
    ans += fs[h][w]
    occupied[p] = 0
    q = deque([])
    q.append(p)
    while len(q) > 0: #pを起点にBFS
        p_ = q.popleft()
        h, w = hw(p_)
        dist = fs[h][w]
        for dh,dw in mvs:
            if 0<=h+dh<n and 0<=w+dw<n:
                # ある位置h,wが
                # occupiedなら、隣接マス+1で到達
                # vacantなら、隣接マス+0で到達
                if dist+occupied[p_] < fs[h+dh][w+dw]:
                    q.append(toP(h+dh,w+dw))
                    fs[h+dh][w+dw] = dist + occupied[p_]                
print(ans)
    
    