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:
        val1=0
        val2=0
        val3=0
        val4=0
        num1=0
        num2=0
        num3=0
        num4=0
        for h in range(r):
            if l1[h][c]!=0:
                val1=1
                num1=num1+1
        for h in range(r+1,n):
            if val1==0:
                break
            if l1[h][c]!=0:
                val2=1
                num2=num2+1
                if num2>=num1:
                    break
        for h in range(c):
            if val1==0 or val2==0:
                break
            if l1[r][h]!=0:
                val3=1
                num3=num3+1
                if num3>=num1 or num3>=num2:
                    break
        for h in range(c+1,n):
            if val1==0 or val2==0 or val3==0:
                break
            if l1[r][h]!=0:
                val4=1
                num4=num4+1
                if num4>=num3 or num4>=num2 or num4>=num1:
                    break
        if val1==1 and val2==1 and val3==1 and val4==1:
            l9=[]
            l9.append(num1)
            l9.append(num2)
            l9.append(num3)
            l9.append(num4)
            cnt=cnt+min(l9)
        l1[r][c]=0
print(cnt)