#!/usr/bin/env python3
import sys

input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

from collections import deque

def main():
    N = INT()
    P = LI()
    adjlist = [[1,1,1,1] for i in range(N**2)]

    def isGoal(node):
        return node//N == 0 or node//N == N-1 or node%N == N-1 or node%N == 0

    output = 0

    for p in P:
        min_cost = 10**9
        queue = deque([(p-1,0)])
        cost_mat = [N]*N**2

        while queue:
            node,cost = queue.popleft()
            if isGoal(node):
                min_cost = cost
                break
            
            if cost >= min_cost or cost_mat[node] <= cost:
                continue

            cost_mat[node] = cost
                        
            for direction in range(4):
                if adjlist[node][direction] == 0:
                    if direction == 0:
                        queue.appendleft((node-N,cost))
                    elif direction == 1:
                        queue.appendleft((node+N,cost))
                    elif direction == 2:
                        queue.appendleft((node-1,cost))
                    else:
                        queue.appendleft((node+1,cost))
                else:
                    if direction == 0:
                        queue.append((node-N,cost+1))
                    elif direction == 1:
                        queue.append((node+N,cost+1))
                    elif direction == 2:
                        queue.append((node-1,cost+1))
                    else:
                        queue.append((node+1,cost+1))
        
        node = p-1
        if node//N > 0:
            adjlist[node-N][1] = 0
        if node//N < N-1:
            adjlist[node+N][0] = 0
        if node%N > 0:
            adjlist[node-1][3] = 0
        if node%N < N-1:
            adjlist[node+1][2] = 0
        
        output += min_cost


    print(output)


if __name__ == '__main__':
    main()