from sys import setrecursionlimit
setrecursionlimit(10**6)
import numpy as np


cimport numpy as np
DTYPE = np.int
ctypedef np.int_t DTYPE_t
def main():
    cdef int n=int(input())
    cdef list p=list(map(int,input().split()))
    cdef int i,j,x,y,t
    cdef DTYPE_t [:, :] ans
    cdef DTYPE_t [:, :] c
    ans=np.zeros([n+1, n+1], DTYPE)
    c=np.ones([n+1, n+1], DTYPE)
    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 [(1,0),(0,1),(-1,0),(0,-1)]:
            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)
    t=0
    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)
    return t
if __name__ == "__main__":
    print(main())