import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
import time
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
#    n = 500
    cos = [min(i//n,n-1-i//n,i%n,n-1-i%n) for i in range(n**2)]
#    p = [i for i in range(n**2)]
    p = [int(i)-1 for i in ipt().split()]
    ans = 0
    hq = []
    dr = [-1,-n,1,n]
    al = [False]*(n**2)

    for pi in p:
        ans += cos[pi]
        hpq.heappush(hq,(cos[pi],pi))
        al[pi] = True
        while hq:
            qc,qi = hpq.heappop(hq)
            ad = qc if al[qi] else qc+1
            for d in dr:
                if 0 <= d+qi < n**2 and cos[d+qi] > ad:
                    hpq.heappush(hq,(ad,d+qi))
                    cos[d+qi] = ad
    print(ans)

    return

if __name__ == '__main__':
    main()
