#################################3
#################################3
#################################3
#################################3


def read(data_type=int, container_type=list):
    return container_type(data_type(x) for x in input().split())


import collections

N, = read()
P = read()


def get_rc(x):
    return x // N, x % N


grid = [[True] * N for _ in range(N)]


def nei(r, c):
    for dr, dc in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        r2 = r + dr
        c2 = c + dc
        if 0 <= r2 < N and 0 <= c2 < N:
            yield (r2, c2)


def find_min_dist(r, c):
    visited_dist = {(r, c): 0}
    q = collections.deque([(r, c)])
    while q:
        r, c = q.popleft()
        if r == 0 or r == N - 1 or c == 0 or c == N - 1:
            return visited_dist[(r, c)]
        for r2, c2 in nei(r, c):
            if (r2, c2) not in visited_dist:
                if grid[r2][c2]:
                    visited_dist[(r2, c2)] = 1 + visited_dist[r, c]
                    q.append((r2, c2))
                else:
                    visited_dist[r2, c2] = visited_dist[r, c]
                    q.appendleft((r2, c2))


sol = 0
for x in P:
    r, c = get_rc(x - 1)
    grid[r][c] = False
    sol += find_min_dist(r, c)

print(sol)

#################################3
#################################3
#################################3
#################################3
