n=int(input())
l=list(map(int, input().split()))
l1=[[0 for i in range(n)] for j in range(n)]
k=1
cnt=0
for i in range(n):
    for j in range(n):
        l1[i][j]=k
        k=k+1
for i in l:
    r=i/n
    if r!=int(r):
        r=int(r)
    else:
        r=int(r)-1
    c=(i%n)-1
    if c==-1:
        c=n-1
    if r==0 or r==n-1 or c==0 or c==n-1:
        l1[r][c]=0
    else:
        val=0
        num1=0
        num2=0
        num3=0
        num4=0
        for h in range(r):
            if l1[h][c]!=0:
                val=1
                num1=num1+1
        if val==1:
            val=0
            for h in range(r+1,n):
                if l1[h][c]!=0:
                    val=1
                    num2=num2+1
                    if num2>num1:
                        break
            if val==1:
                val=0
                for h in range(c):
                    if l1[r][h]!=0:
                        val=1
                        num3=num3+1
                        if num3>num1 or num3>num2:
                            break
                if val==1:
                    val=0
                    for h in range(c+1,n):
                        if l1[r][h]!=0:
                            val=1
                            num4=num4+1
                            if num4>num3 or num4>num2 or num4>num1:
                                break
                    if val==1:
                        l9=[]
                        l9.append(num1)
                        l9.append(num2)
                        l9.append(num3)
                        l9.append(num4)
                        cnt=cnt+min(l9)
        l1[r][c]=0
print(cnt)