# AGC044 B Joker
from collections import deque
import heapq

n = int(input())
p_dq = deque([int(x) for x in input().split()])

def is_edge(i, n):
    _ = i%n
    if i < n or n**2-n <= i or _==0 or _==n-1:
        return True
    else:
        return False

def dijkstra(s, n, c_list):
    _list = [float("Inf")]*(n**2)
    _list[s] = 0
    _min = n
    hq = [[0,s]]
    heapq.heapify(hq)
    while len(hq) > 0:
        _ci, _i = heapq.heappop(hq)
        if _list[_i] < _ci:
            continue
        for _cj,_j in c_list[_i]:
            if _list[_j] > (_list[_i] + _cj):
                _list[_j] = _list[_i] + _cj
                if is_edge(_j,n):
                    _min = min(_min, _list[_j])
                    if _min == 0:
                        return 0
                heapq.heappush(hq, [_list[_j],_j])
    return _min

c_list = [[] for i in range(n**2)]

for i in range(n**2):
    if not(is_edge(i, n)):
        c_list[i].append([1,i-1])
        c_list[i].append([1,i+1])
        c_list[i].append([1,i-n])
        c_list[i].append([1,i+n])
    
ans = 0

for i in range(n**2):
    _tmp = p_dq.popleft() - 1
    if not(is_edge(_tmp, n)):
        ans += dijkstra(_tmp, n, c_list)
    if not(is_edge(_tmp-1,n)):
        c_list[_tmp-1][1][0] = 0
    if not(is_edge(_tmp+1,n)):
        c_list[_tmp+1][0][0] = 0
    if not(is_edge(_tmp-n,n)):
        c_list[_tmp-n][3][0] = 0
    if not(is_edge(_tmp+n,n)):
        c_list[_tmp+n][2][0] = 0

print(ans)