N=int(input())
P=input().split(' ')
for i in range(N**2):
    P[i]=int(P[i])
M=[[1] * N for i in range(N)]
Hate=0
for ij in range(N**2):
    j=(int(P[ij])-1)%N
    i=(int(P[ij])-1)//N
    K1=0
    K2=0
    K3=0
    K4=0
    M[i][j]=0
    for k in range(0,i):
        if M[k][j]==1:
            K1+=1
    for k in range(i,N):
        if M[k][j]==1:
            K2+=1
    for k in range(0,j):
        if M[i][k]==1:
            K3+=1
    for k in range(j,N):
        if M[i][k]==1:
            K4+=1
    Hate+=min(K1,K2,K3,K4)
print(Hate)

