# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
# import math
# from itertools import product, accumulate, combinations, product
# import bisect# lower_bound etc
import numpy as np
# from copy import deepcopy
from collections import deque
import numba

N = int(input())
P = np.array(list(map(int, sysread().split())))
#@numba.njit("void(i8,i8[:])")
@numba.njit
def run():
    seats = np.zeros((N,N), dtype=np.int32)
    seen = np.zeros((N,N), dtype=np.int32)  # there is a person : 1
    up, down = 0, N - 1
    dist = 0
    while True:
        seats[up, up:down+1] = dist
        seats[down, up:down+1] = dist
        seats[up:down+1, up] = dist
        seats[up:down+1, down] = dist
        up += 1
        down -= 1
        dist += 1
        if up > down:
            break

    ret = 0
    ###
    for i in range(N ** 2):
        # print(seats)
        cur_i, cur_j = (P[i] - 1) // N, (P[i] - 1) % N
        ret += seats[cur_i,cur_j]
        seen[cur_i,cur_j] = 1
        stack = [(seats[cur_i,cur_j], (cur_i, cur_j))]
        while stack:
            del_val, (cur_i, cur_j) = stack.pop()
            for i, j in ((1, 0), (0, 1), (-1, 0), (0, -1)):
                n_i = cur_i + i
                n_j = cur_j + j
                if 0 <= n_i <= N - 1 and 0 <= n_j <= N - 1:
                    if seen[cur_i, cur_j]:
                        if del_val < seats[n_i, n_j]:
                            seats[n_i, n_j] = del_val
                            stack.append((del_val, (n_i, n_j)))
                    else:
                        if del_val + 1 < seats[n_i, n_j]:
                            seats[n_i, n_j] = del_val + 1
                            stack.append((del_val, (n_i, n_j)))

    print(ret)


if __name__ == "__main__":
    run()