from math import inf
from collections import defaultdict


def update(current, weight, visited):
    tmp_w = weight[current]
    x, y = current
    next_coords = (
        (x, y + 1),
        (x + 1, y),
        (x - 1, y),
        (x, y - 1)
    )
    for coord in next_coords:
        if (
            coord in visited
            or not (-1 < coord[0] < N)
            or not (-1 < coord[1] < N)
            ):
            continue
        elif weight[coord] > tmp_w:
            weight[coord] = tmp_w + table[coord[0]][coord[1]]


def process(p):
    p -= 1
    x, y = (p // N, p % N)
    weight = defaultdict(lambda: inf)
    visited = set()
    weight[(x, y)] = 0
    table[x][y] = 0
    while x not in [0, N-1] and y not in [0, N-1]:
        update((x, y), weight, visited)
        visited.add((x, y))
        weight.pop((x, y))
        x, y = min(weight.keys(), key=lambda x: weight.get(x))
    return weight[(x, y)]

N = int(input())
P = (int(s) for s in input().split())
table = [[1] * N for _ in range(N)]
print(sum(process(i) for i in P))