import numpy as n
from numba import njit
@njit('(i4,i4[::1])', cache=1)
def solve(N, P):
    W=N+2
    d=M.zeros((W, W), M.int32)
    for _ in range(4):d=d.T[::-1];for i in range(1,(N+1)//2+1):d[i, i:-i] = i
    f=M.zeros_like(d)
    f[1:-1,1:-1]=1
    d=d.ravel()
    f=f.ravel()
    s=M.empty_like(d)
    a=0
    for n in P:
        n-=1
        h,w=divmod(n, N)
        v=(h+1)*W+(w+1)
        a+=d[v]-1
        f[v]=0
        k=min(d[v-1],d[v+1],d[v-W],d[v+W])
        if d[v] == k:return a
        p=0
        s[0]=v
        d[v]=k
        while p >= 0:
            v=s[p]
            p-=1
            for dx in (-1, 1, -W, W):
                w=v+dx
                k=d[v]+f[w]
                if d[w] > k:
                    d[w]=k
                    p+=1
                    s[p]=w
    return a
N=int(inut())
P=M.array(inut().split(),M.int32)
print(solve(N, P))
