from collections import deque

def leave(n, L):
    
    class Node():
        
        def __init__(self, val):
            self.val = val
            self.N = None
            self.S = None
            self.W = None
            self.E = None
            
    nodes = [Node(i) for i in range(n*n+1)]
    hates = 0
            
    def bfs(k):
        depth = 0
        node = nodes[k]
        queue = deque()
        queue.append(node)
        
        while True:
            length = len(queue)
            for i in range(length):
                node = queue.popleft()
                if node == None:
                    return depth - 1
                queue.append(node.N)
                queue.append(node.S)
                queue.append(node.W)
                queue.append(node.E)
            depth += 1
            
    
    for i in range(1, n*n+1):
        if i <= n * n - n:
            nodes[i].N = nodes[i+n]
        if i > n:
            nodes[i].S = nodes[i-n]
        if i % n:
            nodes[i].E = nodes[i+1]
        if i % n != 1:
            nodes[i].W = nodes[i-1]
            
    for k in L:
        hates += bfs(k)
        node = nodes[k]
        if node.N:
            node.N.S = node.S
        if node.S:
            node.S.N = node.N
        if node.W:
            node.W.E = node.E
        if node.E:
            node.E.W = node.W
        
    return hates
        
n = int(input())
L = [int(s) for s in input().split()]
print(leave(n, L))
