def main():
    cdef:
        int n=int(input())
        list p=list(map(int,input().split()))
        list ans=[0]*((n+1)**2)
        list c=[1]*((n+1)**2)
        int t=0
        list di=[1,-1,n+1,-n-1]
        int i,j,x,y,k
    import sys
    sys.setrecursionlimit(10**6)
    for i in range(1,n+1):
        for j in range(1,n+1):
            k=i*(n+1)+j
            ans[k]=min(i-1,j-1,n-i,n-j)
    def f(int k):
        for i in di:
            if k+i>=(n+1)**2:
                continue
            if ans[k]+c[k]<ans[k+i]:
                    ans[k+i]-=1
                    f(k+i)
    for i in p:
        y=i%n
        if y==0:
            y=n
        x=(i+n-1)//n
        k=x*(n+1)+y
        c[k]=0
        t+=ans[k]
        f(k)
    print(t)
main()