import sys
input = sys.stdin.readline
 
n = int(input())
P = list(map(int,input().split()))
for i in range(n**2):
    P[i] -= 1
 
 
M = [[] for i in range(n**2)]
DD = [[-1,0],[1,0],[0,1],[0,-1]]
V = [1] * (n**2)
for i in range(n**2):
    x = i//n
    y = i%n
    for d in range(4):
        gx = x + DD[d][0]
        gy = y + DD[d][1]
        if 0 <= gx < n and 0 <= gy < n:
            g = gx*n + gy
            M[i].append(g)
 
D = [0] * (n**2)
for i in range(n**2):
    x = i//n
    y = i%n
    D[i] = min(x, n-1-x, y, n-1-y)
 
ans = 0
for i in range(n**2):
    # print(D)
    ans += D[P[i]]
    V[P[i]] = 0
    
    s = 0
    Q = [P[i]]
    while s < len(Q):
        mi = D[Q[s]]
        for j in M[Q[s]]:
            if D[j] > D[Q[s]] + V[Q[s]]:
                Q.append(j)
                D[j] = D[Q[s]] + V[Q[s]]
        s += 1
print(ans)