from numba import njit
import numpy as np
@njit
def func(x,c,h,stay):
    # hを更新
    todo=[[x,c]]
    seen={x}
    while todo:
        v,c=todo.pop()
        nvs=[v+n,v-n]
        if v%n<n-1:nvs.append(v+1)
        if v%n>0:nvs.append(v-1)
        for nv in nvs:
            if nv<0 or n**2<=nv:continue
            if nv in seen:continue
            if h[nv]>c:
                h[nv]=c
                todo.append([nv,c+stay[nv]])
            seen.add(nv)

@njit
def main(n,p):
    h=[min(i//n,n-i//n-1,i%n,n-i%n-1) for i in range(n**2)]
    h=np.array(h)
    stay=np.full(n**2,1)
    ans=0

    for x in p:
        c=h[x-1]
        ans+=c
        stay[x-1]=0
        func(x-1,c,h,stay)
    print(ans)

    #for i in range(n):
    #    print(h[i*n:(i+1)*n])
if __name__=='__main__':
    n=int(input())
    p=list(map(int,input().split()))
    main(n,p)