# 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 = [[0] * N for _ in range(N)]
    seen = [[0] * N for _ in range(N)]# there is a person : 1
    left, right, up, down = 0, N-1, 0, N-1
    dist = 0
    while True:
        for i in range(left, right+1):
            seats[up][i] = dist
            seats[down][i] = dist
        for i in range(up, down+1):
            seats[i][left] = dist
            seats[i][right] = dist
        left += 1
        right -= 1
        up += 1
        down -= 1
        dist+= 1
        if left > right or up > down:
            break

    ret = 0
    ###
    for i in range(N**2):
        #print(seats)
        way = {}
        cur_i, cur_j = (P[i] - 1) // N, (P[i] - 1) % N
        ret += seats[cur_i][cur_j]
        seen[cur_i][cur_j] = 1
        del_val = seats[cur_i][cur_j]
        stack = deque([(del_val, (cur_i, cur_j))])

        while stack:
            del_val, (cur_i,cur_j) = stack.popleft()
            way[cur_i * N + cur_j] = 0
            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 and n_i * N + n_j not in way.keys():
                    if del_val < seats[n_i][n_j]:
                        seats[n_i][n_j] = del_val
                        stack.append((del_val + 1, (n_i, n_j)))
                    elif seen[n_i][n_j]:
                        stack.append((del_val, (n_i, n_j)))

    print(ret)


if __name__ == "__main__":
    run()