from numba import njit
n=int(input())
A=list(map(int,input().split()))
@njit
def solve(n,A):
    remain=[[1]*n for i in range(n)]

    que1=[]
    que2=[]
    ans=0
    for a in A:
        x,y=(a-1)//n,(a-1)%n
        remain[x][y]=0
        que2.append((x,y))
        check=[[0]*n for i in range(n)]
        check[x][y]=1
        endflag=0
        for i in range(0,501):
            newque=[]
            while que1:
                x,y=que1.pop()
                for nx,ny in ((x-1,y),(x+1,y),(x,y+1),(x,y-1)):
                    if nx<0 or ny<0 or nx>=n or ny>=n:
                        endflag=1
                        break
                    if check[nx][ny]==0:
                        check[nx][ny]=1
                        if remain[nx][ny]:
                            newque.append((nx,ny))
                        else:
                            que2.append((nx,ny))
            while que2:
                x,y=que2.pop()
                for nx,ny in ((x-1,y),(x+1,y),(x,y+1),(x,y-1)):
                    if nx<0 or ny<0 or nx>=n or ny>=n:
                        endflag=1
                        break
                    if check[nx][ny]==0:
                        check[nx][ny]=1
                        if remain[nx][ny]:
                            newque.append((nx,ny))
                        else:
                            que2.append((nx,ny))
            if endflag:
                break
            que1=newque
        ans+=i
    print(ans)
solve(n,A)