import sys

import heapq

readline = sys.stdin.buffer.readline


def start_point(P):
    j, i = divmod(P, N)
    return i, j


def bfs(start, ch, reached):
    tests = ((1, 0), (-1, 0), (0, 1), (0, -1))
    queue = []
    moves = 0
    heapq.heappush(queue, (start[0], start[1], moves))
    ans = []

    while queue:
        queue_next = []

        for _ in range(len(queue)):
            x, y, o_move = heapq.heappop(queue)
            for dx, dy in tests:
                tx, ty = x + dx, y + dy
                if tx < N and 0 <= tx and ty < N and 0 <= ty:
                    if reached[tx][ty] is False:
                        if ch[tx][ty] is False:
                            reached[tx][ty] = o_move
                            heapq.heappush(queue_next, (tx, ty, o_move))
                        else:
                            reached[tx][ty] = o_move + 1
                            heapq.heappush(queue_next, (tx, ty, o_move + 1))

                else:
                    ans.append(o_move)

        queue = queue_next

    return min(ans)


# 嫌われる人数 = min(通路までの距離, すでに人がいない席までの距離)
# すでに人がいない席までの距離はwfs

N = int(readline())
P = list(map(int, readline().split()))

# O始まりに変換
P = list(map(lambda x: x - 1, P))

cnt = 0

# True: 人がいる
ch = [[True for _ in range(N)] for _ in range(N)]

# 最初の一人は通路までの距離
start = start_point(P[0])
cnt += min(start)
ch[start[0]][start[1]] = False

kk = [0 for _ in range(N * N)]

for p in P[1:]:
    start = start_point(p)

    # 通路までの距離
    m0 = min(start[0], start[1], abs(N - start[0] - 1), abs(N - start[1] - 1))

    if m0 != 0:
        # 人がいない席までの距離
        reached = [[False for _ in range(N)] for _ in range(N)]
        m1 = bfs(start, ch, reached)
    else:
        m1 = 0

    ch[start[0]][start[1]] = False
    cnt += min(m0, m1)

print(cnt)
