def main():
    N=int(input())
    F=[1 for _ in range(N**2)]
    P=list(map(int,input().split()))
    ans = 0
    for p in P:
        F[p-1]=0
        if (p%N == 0) and (p!=N**2):
            ans += min(sum(F[(p-1)%N:(p-1):N]), sum(F[(p-1)+N::N]), sum(F[((p-1)//N)*N:p-1]), sum(F[p:(p//N)*N]))
        else:
            ans += min(sum(F[(p-1)%N:(p-1):N]), sum(F[(p-1)+N::N]), sum(F[((p-1)//N)*N:p-1]), sum(F[p:(p//N+1)*N]))
        # print('上', F[(p-1)%N:(p-1):N])
        # print('下', F[(p-1)+N::N])
        # print('左', F[((p-1)//N)*N:p-1])
        # if (p%N == 0) and (p!=N**2):
        #     print('右', F[p:(p//N)*N])
        # else:
        #     print('右', F[p:(p//N+1)*N])
    print(ans)

main()