
N=int(input())
A=list(map(int,input().strip().split()))

def getCoordinate(n):
    x=n%N
    if x==0:
        x=N
    y=int(n/N)
    if n%N!=0:
        y=y+1
    return x-1,y-1

class Query :
    def __init__(self):
        self.x1=0
        self.y1=0
        self.x2=0
        self.y2=0
#// A function to update the 2D BIT 
def updateBIT(BIT,x,y,val):
    y1=0;
    while(x<=N):
        y1=y
        while(y1<=N):
            BIT[x][y1]+=val
            y1+=(y1 & -y1)
        x += (x & -x)
#def updateBIT(BIT, x,  y, val) :
#    M=N
#    while(x<=M):
#        while(y<=M):
#            BIT[x][y]+=val
#            y+= (y& -y)
#        x+=(x& -x)
#    return; 
  
#// A function to get sum from (0, 0) to (x, y) 
def getSum( BIT, x,  y) :
    sum = 0; 
    y1=0
    while(x>0):
        y1=y
        while(y1>0):
            sum += BIT[x][y1]; 
            y1 -= (y1 &-y1)
        x -= x&-x        
    return sum; 
  
#// A function to create an auxiliary matrix 
#// from the given input matrix 
def constructAux( mat, aux):  
    #// Initialise Auxiliary array to 0 
    for i in range(N+1):
        for j in range(1,N):
            aux[i][j]=0; 
    #// Construct the Auxiliary Matrix 
    for i in range(N+1):
        for j in range(1,N+1):
            aux[i][j] = mat[N-j][i-1]; 
    return;
  
#// A function to construct a 2D BIT 
def construct2DBIT(mat, BIT):
    #// Create an auxiliary matrix 
    #aux[N+1][N+1]; 
    aux=[]
    for i in range(N+1):
        aux.append([0]*(N+1))
    constructAux(mat, aux)
  
    #// Initialise the BIT to 0 
    for i in range(1,N+1):
    #for (int i=1; i<=N; i++) 
        for j in range(1,N+1):
        #for (int j=1; j<=N; j++) 
            BIT[i][j] = 0; 
    for j in range(1,N+1):  
    #for (int j=1; j<=N; j++) 
        for i in range(1,N+1):  
        #for (int i=1; i<=N; i++)  
           # // Creating a 2D-BIT using update function 
           # // everytime we/ encounter a value in the 
           # // input 2D-array 
            v1 = getSum(BIT, i, j); 
            v2 = getSum(BIT, i, j-1); 
            v3 = getSum(BIT, i-1, j-1); 
            v4 = getSum(BIT, i-1, j); 
  
            #// Assigning a value to a particular element 
            #// of 2D BIT 
            updateBIT(BIT, i, j, aux[i][j]-(v1-v2-v4+v3));   
    return; 

#// A function to answer the queries

def answerQueries(q,BIT):
    #for (int i=0; i<m; i++) 
        x1 =q.x1 + 1; 
        y1 = q.y1 + 1; 
        x2 = q.x2 + 1; 
        y2 = q.y2 + 1; 
     
        ans = getSum(BIT, x2, y2)-getSum(BIT, x2, y1-1)- getSum(BIT, x1-1, y2)+getSum(BIT, x1-1, y1-1); 
        if ans<0:
            return 0
        return ans;   

B=[]
for i in range(N):
    B.append([0]*N)
j=0
for i in range(N):
    for k in range(N):
        B[i][k]=1
        j+=1
#int BIT[N+1][N+1]; 
#    construct2DBIT(mat, BIT); 
BIT=[]
for i in range(N+2):
    BIT.append([0]*(N+2))
construct2DBIT(B,BIT)
cnt=0
q=Query()
for i in range(0,len(A)):
    #print("___________________________________________________")
    p=A[i]
    x,y=getCoordinate(p)
    ans1,ans2,ans3,ans4=0,0,0,0
    if x!=0:
        q.x1=0
        q.x2=x-1
        q.y1=y
        q.y2=y
        #print(q.x1,q.y1,q.x2,q.y2)
        ans1=answerQueries(q,BIT)
    if x!=N-1:
        q.x1=x+1
        q.x2=N-1
        q.y1=y
        q.y2=y
        #ans2=getSum(BIT,3,y)-getSum(BIT,x,y)
        #print(q.x1,q.y1,q.x2,q.y2)
        ans2=answerQueries(q,BIT)
    if y!=0:
        q.x1=x
        q.x2=x
        q.y1=0
        q.y2=y-1
        #print(q.x1,q.y1,q.x2,q.y2)
        ans3=answerQueries(q,BIT)
    if y!=N-1:
        q.x1=x
        q.x2=x
        q.y1=y+1
        q.y2=N-1
        #print(q.x1,q.y1,q.x2,q.y2)
        ans4=answerQueries(q,BIT)
    cnt+=min(ans1,ans2,ans3,ans4)
    updateBIT(BIT,x+1,y+1,-1)
print(cnt)