Problem:
Score : 700 points

Problem Statement

Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2.

At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat.
To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column).
While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever.

Compute the number of pairs of viewers (x, y) such that y will hate x forever.

Constraints

2 \le N \le 500

The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \{1, 2, \dots, N^2\}.

Input

The input is given from Standard Input in the format

N
P_1 P_2 \cdots P_{N^2}

Output

If ans is the number of pairs of viewers described in the statement, you should print on Standard Output

ans

Sample Input 1

3
1 3 7 9 5 4 8 6 2

Sample Output 1

1

Before the end of the movie, the viewers are arranged in the cinema as follows:

1 2 3
4 5 6
7 8 9

The first four viewers leaving the cinema (1, 3, 7, 9) can leave the cinema without going through any seat, so they will not be hated by anybody.

Then, viewer 5 must go through one of the seats where viewers 2, 4, 6, 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers.

Finally the remaining viewers can leave the cinema (in the order 4, 8, 6, 2) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all).

Sample Input 2

4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8

Sample Output 2

3

Sample Input 3

6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30

Sample Output 3

11

Solution in PyPy3 (7.3.0) (judge verdict: Time Limit Exceeded)

def examA():
    T = I()
    ans = [inf]*T
    for t in range(T):
        N, A, B, C, D = LI()
        num = [[2,A],[3,B],[5,C]]
        dp = defaultdict(lambda: inf)
        dp[N] = 0
        que = deque()
        que.append(N)
        while(que):
            now = que.pop()
            if now==0:
                continue
            cur = dp[now] + D*now
            if cur<dp[0]:
                dp[0] = cur
            cur = dp[now]
            for n,K in num:
                curn = cur + (now % n) * D + K
                if curn < dp[(now - now % n) // n]:
                    dp[(now - now % n) // n] = curn
                    que.append((now - now % n) // n)
                curn_ = cur + (-now % n) * D + K
                if curn_ < dp[(now + (-now % n)) // n]:
                    dp[(now + (-now % n)) // n] = curn_
                    que.append((now + (-now % n)) // n)
        ans[t] = dp[0]
    for v in ans:
        print(v)
    return

def examB():
    N = I()
    P = LI()
    L = [[min(i,j,N+1-i,N+1-j)for i in range(1,N+1)]for j in range(1,N+1)]
    exist = [[1]*N for _ in range(N)]
    cost = 0
    def bfs(s):
        que = deque()
        que.appendleft(s)

        def calc_L(h, w, h1, w1):
            c = L[h][w] + exist[h + h1][w + w1]
            if c < L[h + h1][w + w1]:
                L[h + h1][w + w1] = c
                s1 = (h + h1) * N + (w + w1) + 1
                # print("append",s1)
                que.appendleft(s1)
            return

        while(que):
            now = que.pop()
            h,w = (now-1)//N,(now-1)%N
            if 0<h:
                calc_L(h,w,-1,0)
            if h+1<N:
                calc_L(h,w,1,0)
            if 0<w:
                calc_L(h,w,0,-1)
            if w+1<N:
                calc_L(h,w,0,1)

        return

    #for l in L:
        #print(l)
    #print("start")
    for p in P:
        h,w = (p-1)//N,(p-1)%N
        exist[h][w] = 0
        if 0<L[h][w]:
            L[h][w] -= 1
        bfs(p)
        cost += L[h][w]
        #print(p,cost)
    #for l in exist:
        #print(l)
    ans = cost
    print(ans)
    return

def examB2():
    N = I()
    P = LI()
    L = [min(i,j,N+1-i,N+1-j) for i in range(1,N+1) for j in range(1,N+1)]
    exist = [1]*(N**2)
    cost = 0
    def bfs(s):
        que = deque()
        que.appendleft(s)

        def calc_L(s, s0):
            c = L[s] + exist[s+s0]
            if c < L[s+s0]:
                L[s+s0] = c
                s1 = s+s0
                # print("append",s1)
                que.appendleft(s1)
            return

        while(que):
            now = que.pop()
            if 0<s-1:
                calc_L(h,w,-1,0)
            if h+1<N:
                calc_L(h,w,1,0)
            if 0<w+1:
                calc_L(h,w,0,-1)
            if w+1<N:
                calc_L(h,w,0,1)

        return

    #for l in L:
        #print(l)
    #print("start")
    for p in P:
        h,w = (p-1)//N,(p-1)%N
        exist[h][w] = 0
        if 0<L[h][w]:
            L[h][w] -= 1
        bfs(p)
        cost += L[h][w]
        #print(p,cost)
    #for l in exist:
        #print(l)
    ans = cost
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return


import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""