import math

def find(x,n):
    row = int(math.ceil(x/n))
    x %= n
    if x == 0:
        col = n
    else:
        col = x

    return row,col 

def sumQuery(BIT,index):
    if index > len(BIT)-1:
        return 0
    total = 0
    while index > 0:
        #print(index)
        total += BIT[index]
        index -= index&-index

    return total

def update(BIT,index,val):
    while index < len(BIT):
        BIT[index] += val
        index += index&-index

def main():
    n = int(input())
    arr = list(map(int,input().split()))

    row_BITS = [[0 for j in range(n+1)] for i in range(n)]
    col_BITS = [[0 for j in range(n+1)] for i in range(n)]
    for i in range(n):
        for j in range(1,n+1):
            update(row_BITS[i],j,1)
            update(col_BITS[i],j,1)

    ans = 0
    for i in arr:
        row,col = find(i,n)
        left = sumQuery(row_BITS[row-1],col-1)
        right = sumQuery(row_BITS[row-1],n)-sumQuery(row_BITS[row-1],col)
        up = sumQuery(col_BITS[col-1],row-1)
        down = sumQuery(col_BITS[col-1],n)-sumQuery(col_BITS[col-1],row)
        min_val = min(left,right,up,down)
        ans += min_val
        update(row_BITS[row-1],col,-1)
        update(col_BITS[col-1],row,-1)
        
    print(ans)

main()
