n=int(input())
p=list(map(int,input().split()))
seat=['0'*n]*n
answer=0
for i in range(n**2):
    row=p[i]//n
    if p[i]%n==0:
        row-=1
    lin=p[i]%n-1
    if lin<0:
        lin=n-1
    hate=min(seat[row][:lin].count('0'),seat[row][lin+1:].count('0'))
    t=0
    s=0
    for j in range(n-1):
        if j<row:
            if seat[j][lin]=='0':
                t+=1
        if n-j-1>row:
            if seat[-j-1][lin]=='0':
                s+=1
    answer+=min(hate,t,s)
    seat[row]=seat[row][:lin]+'1'+seat[row][lin+1:]
print(answer)