# 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 = [[1] * N for _ in range(N)]# there is a person : 1

    def heap(i):
        stack = []
        current = ((P[i] - 1) // N, (P[i] - 1) % N)
        heappush(stack, (0, current))
        seats[current[0]][current[1]] = 0
        seen = [[0] * N for _ in range(N)]
        seen[current[0]][current[1]] = 1
        while stack:
            count, (current_i, current_j) = heappop(stack)
            if current_i == 0 or current_i == N-1 or current_j == 0 or current_j == N-1:
                break
            seen[current_i][current_j] = 1
            for i,j in ((1,0),(0,1),(-1,0),(0,-1)):
                next_i = current_i + i
                next_j = current_j + j
                if 0 <= next_i <= N-1 and 0 <= next_j <= N-1 and not seen[next_i][next_j]:
                    heappush(stack, (count + seats[next_i][next_j], (next_i, next_j)))
        return count

    ret = 0
    for i in range(N**2):
        ret += heap(i)
    print(ret)


if __name__ == "__main__":
    run()
