N=int(input())
P=list(map(int,input().split()))
A=[]
ans=0
for i in range(0,N):
    for j in range(0,N):
        A.append([j,i,N-j-1,N-i-1])
for p in P:
    count=max(min(A[p-1]),0)
    if count!=0:
        ans+=count
    x=(p-1)//N
    y=(p-1)%N
    low=[]
    high=[]
    down=[]
    up=[]
    for i in range(N * x, N * x+N):
        if p-1>i:
            low.append(i)
        elif p-1<i:
            high.append(i)
    for l in low:
        A[l][2]-=1
    for h in high:
        A[h][0]-=1
    for j in range(0, N):
        if p-1>y+j*N:
            down.append(y+j*N)
        elif p-1<y+j*N:
            up.append(y+j*N)
    for d in down:
        A[d][3]-=1
    for u in up:
        A[u][1]-=1
print(ans)