input_list = lambda : list(map(int,input().split()))

#def rec(n,total):


n=int(input())
p=input_list()
left=[[i for i in range(n)] for _ in range(n)]
right=[[n-i-1 for i in range(n)] for _ in range(n)]
up=[[i for _ in range(n)] for i in range(n)]
down=[[n-i-1 for _ in range(n)] for i in range(n)]
ans=0
for each in p:
    each-=1
    row=each//n
    line=each%n
    tmp=min(up[row][line],down[row][line],left[row][line],right[row][line])
    ans+=tmp
    #
    # print(right)
    #print(row,line,"->",tmp)
    #print()

    for i in range(line,n):
        left[row][i]=max(0,left[row][i]-1)
    for i in range(line,-1,-1):
        right[row][i]=max(0,right[row][i]-1)
    for i in range(row,n):
        up[i][line]=max(0,up[i][line]-1)
    for i in range(row,-1,-1):
        down[i][line]=max(0,down[i][line]-1)

    

print(ans)