import numpy as np

def resolve(N,Ps):
    dislike_counter = 0

    seats = np.ones(N*N, dtype=np.int64)
    seats_sq = seats.reshape((N,N))

    for p in Ps:
        no = p-1
        seats[no] = 0
        i, j = no // N, no % N 

        up = seats_sq[:i,j].sum()
        down = seats_sq[i+1:, j].sum()
        left = seats_sq[i, :j].sum()
        right = seats_sq[i, j+1:].sum()

        dislike_counter += min((up, down, left, right))
        
    return dislike_counter


def main():
    N = int(input())
    Ps = list(map(int, input().split()))
    ans = resolve(N, Ps)

    print(ans)

if __name__ == '__main__':
    main()