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

def hw(p):
    h = (p-1)//n
    w = (p-1)%n
    return h,w

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

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)]
vacant = [[False]*n for i in range(n)]

for p in Ps:
    h,w = hw(p) 
    # 自分と、周りの中で最小のものを選ぶ
    a = fs[h][w]
    for dh,dw in mvs:
        if 0<=h+dh<n and 0<=w+dw<n:
            if vacant[h+dh][w+dw]:
                a = min(a, fs[h+dh][w+dw])
            else:
                a = min(a, fs[h+dh][w+dw] + 1)       
    ans += a
    # print(p,"hw",h,w,"dist",a)
    vacant[h][w] = True
    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:
                if vacant[h+dh][w+dw]: #隣がvacant
                    if dist < fs[h+dh][w+dw]:
                        q.append(toP(h+dh,w+dw))
                        fs[h+dh][w+dw] = dist
                else: #隣がoccupied
                    if dist + 1 < fs[h+dh][w+dw]:
                        q.append(toP(h+dh,w+dw))
                        fs[h+dh][w+dw] = dist + 1 
    
    # for r in fs:
    #     print(*r)
    # for p in vacant:
    #     print(*p)
    # print("---------")
                        
                
print(ans)
    
    