from collections import deque
import sys
sys.setrecursionlimit(10**7)

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))
INF = 10**18

minDist = [[INF] * N for _ in range(N)]
isFilled = [[True] * N for _ in range(N)]


def dfs(h, w):
    dist = minDist[h][w] + (1 if isFilled[h][w] else 0)
    for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
        if 0 <= toH < N and 0 <= toW < N:
            if minDist[toH][toW] > dist:
                minDist[toH][toW] = dist
                dfs(toH, toW)

for i in range(N):
    minDist[i][0] = 0
    minDist[0][i] = 0
    minDist[i][N - 1] = 0
    minDist[N - 1][i] = 0
for i in range(N):
    dfs(i, 0)
    dfs(0, i)
    dfs(i, N - 1)
    dfs(N - 1, i)

ans = 0
for h, w in P:
    ans += minDist[h][w]
    isFilled[h][w] = False
    dfs(h, w)
print(ans)
