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