import math
import heapq
from collections import deque


class Graph:
    def __init__(self, n):
        self.vertex = list(range(1, n*n+1)) + ['g']
        self.edge = {}
        self.adjacent = {v: [] for v in self.vertex}
        self.nuke = {v: False for v in self.vertex}
        for i in range(1, n*(n-1)+1):
            self.add_edge(i, i+n, 1)
        for i in range(1, n*n+1):
            if i%n != 0:
                self.add_edge(i, i+1, 1)
        for i in range(1, n*n+1):
            if i <=  n or i % n == 1 or i % n == 0 or i > n * (n-1):
                self.add_edge(i, 'g', 1)

    def add_edge(self, tail, head, weight):
        self.adjacent[tail].append(head)
        self.edge[tail, head] = weight
        if head != 'g':
            self.adjacent[head].append(tail)
            self.edge[head, tail] = weight

def bfs(G, source, target):
    distance = {v: math.inf for v in G.vertex}
    distance[source] = 0
    reached = {k: False for k in G.vertex}
    reached[source] = True
    queue = deque([source])
    while queue:
        u = queue.popleft()
        for v in G.adjacent[u]:
            if not reached[v]:
                reached[v] = True
                if G.nuke[v]:
                    distance[v] = distance[u]
                else:
                    distance[v] = distance[u] + 1
                queue.append(v)
            else:
                if G.nuke[v]:
                    alt = distance[u]
                else:
                    alt = distance[u] + 1
                if distance[v] > alt:
                    distance[v] = alt
    return distance[target] - 1


n = int(input())
P = map(int, input().split())
G = Graph(n)
ans = 0
for p in P:
    ca = bfs(G, p, 'g')
    ans += ca
    G.nuke[p] = True
print(ans)