from collections import deque

N = int(input())

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

P = list(map(pToHW, input().split()))

isFilled = [[False] * N for _ in range(N)]

def calcDist():
    minDist = [[10**18] * N for _ in range(N)]
    que = deque([])
    for h in range(N):
        if isFilled[h][0]:
            que.append((h, 0, 1))
        else:
            que.appendleft((h, 0, 0))
        if isFilled[h][N - 1]:
            que.append((h, N - 1, 1))
        else:
            que.appendleft((h, N - 1, 0))
    for w in range(1, N - 1):
        if isFilled[0][w]:
            que.append((0, w, 1))
        else:
            que.appendleft((0, w, 0))
        if isFilled[N - 1][w]:
            que.append((N - 1, w, 1))
        else:
            que.appendleft((N - 1, w, 0))

    while que:
        h, w, dist = que.popleft()

        if minDist[h][w] <= dist:
            continue
        minDist[h][w] = dist

        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if not (0 <= toH < N and 0 <= toW < N):
                continue

            if isFilled[toH][toW]:
                que.append((toH, toW, dist + 1))
            else:
                que.append((toH, toW, dist))
    return minDist

ans = 0
for h, w in P[::-1]:
    ans += calcDist()[h][w]
    isFilled[h][w] = True
print(ans)
