def main():
    N  = int(input()) 
    P = list(map(int, input().split()))  
    M = []
    MAP = []
    NN = N*N
    for i in range(N):
        MAP.append(list([0]*N))
#        M.append(range(N*i,N*(i+1)))
#        MAP.append(P[N*i:(N*(i+1))])


    for i in range(NN):
        X = (P[i]-1) % N
        Y = (P[i]-1) // N
        MAP[Y][X] = i

    P1 = [0]*(N*N)
    P2 = [0]*(N*N)
    P3 = [0]*(N*N)
    P4 = [0]*(N*N)
    cnt = 0

    for i in range(NN):
        X = i % N
        Y = i // N
        for j in range(X+1,N):
            if(MAP[Y][X]>MAP[Y][j]):
                P1[Y*N+j] +=1
        for j in range(X-1,-1,-1):
            if(MAP[Y][X]>MAP[Y][j]):
                P2[Y*N+j] +=1
        for j in range(Y+1,N):
            if(MAP[Y][X]>MAP[j][X]):
                P3[j*N+X] +=1
        for j in range(Y-1,-1,-1):
            if(MAP[Y][X]>MAP[j][X]):
                P4[j*N+X] +=1
    
    for i in range(NN):
        cnt += min(P1[i],P2[i],P3[i],P4[i])

    print(cnt)





if __name__ == '__main__':
    main()