def main():
    N = int(input())
    P = [int(i)-1 for i in input().split(' ')]
    seat = [[1 for j in range(N)] for i in range(N)]
    score = 0
    for i in range(len(P)):
        left = sum(seat[P[i]//N][:P[i]%N])
        right = sum(seat[P[i]//N][P[i]%N:])-1 if sum(seat[P[i]//N][P[i]%N:])-1>=0 else 0
        top = 0
        j = P[i]//N
        while True:
            if j >= 0:
                top += seat[j][P[i]%N]
            else:
                break
            j -= 1
        bottom = 0
        j = P[j]%N
        while True:
            if j >= 0:
                bottom += seat[P[i]//N][j]
            else:
                break
            j -= 1
        score_direction = [left, right, top, bottom]
        score += min(score_direction)
        seat[P[i]//N][(P[i]-1)%N] = 0
    print(score)

if __name__ == "__main__":
    main()