### BUGGY CODE ###

# 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
def run():
    N = int(input())
    P = list(map(int, sysread().split()))
    seats = [[1] * N for _ in range(N)]# there is a person : 1

    def heap(i):
        stack = []
        current = ((P[i] - 1) // N, (P[i] - 1) % N)
        heappush(stack, (0, current))
        seats[current[0]][current[1]] = 0
        seen = [[0] * N for _ in range(N)]
        seen[current[0]][current[1]] = 1
        while stack:
            count, (current_i, current_j) = heappop(stack)
            seen[current_i][current_j] = 1
            for i,j in ((1,0),(0,1),(-1,0),(0,-1)):
                next_i = current_i + i
                next_j = current_j + j
                if next_i < 0 or next_i > N-1 or next_j < 0 or next_j > N-1:
                    stack = []
                    break
                if not seen[next_i][next_j]:
                    heappush(stack, (count + seats[next_i][next_j],(next_i, next_j)))
        return count

    ret = 0
    for i in range(N**2):
        ret += heap(i)
    print(ret)


if __name__ == "__main__":
    run()


### FIXED CODE ###

# 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

def run():
    N = int(input())
    P = np.array((list(map(int, sysread().split()))))
    P -= 1

    @numba.njit("void(i8,i8[:])")
    def proc(N, P):
        ans = 0
        Removed = np.zeros((500, 500), dtype=np.int8)
        Mat = np.zeros((500, 500), dtype=np.int16)
        Dy, Dx = [1, 0, -1, 0], [0, 1, 0, -1]

        for y in range(N):
            for x in range(N):
                Mat[y, x] = min(y, N - 1 - y, x, N - 1 - x)
        # print(Mat)
        for i in range(N * N):
            p = P[i]
            y, x = divmod(p, N)
            ans += Mat[y, x]
            Removed[y, x] = 1
            st = [(y, x)]
            while st:
                vy, vx = st.pop()
                vr = Removed[vy, vx]
                val = Mat[vy, vx]
                # for dy, dx in zip(Dy, Dx):
                for j in range(4):
                    uy, ux = vy + Dy[j], vx + Dx[j]
                    if not (0 <= uy < N and 0 <= ux < N):
                        continue
                    ur = Removed[uy, ux]
                    if vr:
                        if Mat[uy, ux] >= val + 1:
                            Mat[uy, ux] = val
                            st.append((uy, ux))
                    else:
                        if Mat[uy, ux] >= val + 2:
                            Mat[uy, ux] = val + 1
                            st.append((uy, ux))
        # for m in Mat:
        #     print(m)
        print(ans)

    proc(N, P)


if __name__ == "__main__":
    run()