N = int(input())

P = [int(i) for i in input().split()]

seat = [[1 for _ in range(N)] for _ in range(N)]

count = 0
for i in range(N*N):
    row = (P[i]-1) // N
    column = (P[i]-1) % N



    if(row==0 or row==N-1 or column==0 or column==N-1):
        pass
    else:
        left = sum(seat[row][0:column])
        right = sum(seat[row][column+1:N])
        up = sum([l[column] for l in seat[0:row]])
        down = sum(l[column] for l in seat[row+1:N])
        count += min(left, right, up, down)

    seat[row][column] = 0


print(count)