def main():
    n=int(input())
    Num=[0]*(n**2+1)
    Exited=[0]*(n**2+1)
    X=tuple(map(int,input().split()))
    for i in range(n**2):
        q,r=divmod(i,n)
        Num[i+1]=min(q,n-q-1,r,n-r-1)
    ans=0
    for x in X:
        ans+=Num[x]
        Exited[x]=1
        Stack=[x]
        while Stack:
            v=Stack.pop()
            if v>n and (Num[v]<Num[v-n] and (Exited[v] or Num[v-n]-Num[v]>1)):
                Num[v-n]-=1
                Stack.append(v-n)
            if v<=n**2-n and (Num[v]<Num[v+n] and (Exited[v] or Num[v+n]-Num[v]>1)):
                Num[v+n]-=1
                Stack.append(v+n)
            if v%n!=0 and (Num[v]<Num[v+1] and (Exited[v] or Num[v+1]-Num[v]>1)):
                Num[v+1]-=1
                Stack.append(v+1)
            if v%n!=1 and (Num[v]<Num[v-1] and (Exited[v] or Num[v-1]-Num[v]>1)):
                Num[v-1]-=1
                Stack.append(v-1)
    print(ans)

if __name__=='__main__':
    main()