from collections import defaultdict
from collections import deque
import math
N = int(input())
P = list(map(int, input().split()))

map = []
for i in range(N):
    map.append([])
    for j in range(N):
        map[i].append(True)

def check(x, y):
    distance = defaultdict(lambda: defaultdict(lambda: 500))
    stack = deque()
    stack.append([x, y])
    distance[x][y] = 0
    min_d = 500

    while len(stack) != 0:
        x_y = stack.popleft()
        new_x = x_y[0]
        new_y = x_y[1]
        distance_xy = distance[new_x][new_y]
        if distance_xy < min_d and (new_x == 0 or new_y == 0 or new_x == N-1 or new_y == N-1):
            min_d = distance_xy

        if x <= new_x:
            if new_x+1 < N:
                if distance[new_x+1][new_y] == 500:
                    stack.append([new_x+1, new_y])
                next_d = distance_xy
                if map[new_x+1][new_y]:
                    next_d += 1
                if next_d < distance[new_x+1][new_y]:
                    distance[new_x+1][new_y] = next_d
        if x >= new_x:
            if 0 <= new_x-1:
                if distance[new_x-1][new_y] == 500:
                    stack.append([new_x-1, new_y])
                next_d = distance_xy
                if map[new_x-1][new_y]:
                    next_d += 1
                if next_d < distance[new_x-1][new_y]:
                    distance[new_x-1][new_y] = next_d
        if y <= new_y:
            if new_y+1 < N:
                if distance[new_x][new_y+1] == 500:
                    stack.append([new_x, new_y+1])
                next_d = distance_xy
                if map[new_x][new_y+1]:
                    next_d += 1
                if next_d < distance[new_x][new_y+1]:
                    distance[new_x][new_y+1] = next_d
        if y >= new_y:
            if 0 <= new_y-1:
                if distance[new_x][new_y-1] == 500:
                    stack.append([new_x, new_y-1])
                next_d = distance_xy
                if map[new_x][new_y-1]:
                    next_d += 1
                if next_d < distance[new_x][new_y-1]:
                    distance[new_x][new_y-1] = next_d
    return min_d

result = 0
for p in P:
    px = (p-1)%N
    py = math.floor((p-1)/N)
    result += check(px, py)
    map[px][py] = False
print(result)
