program key2020_D
    implicit none
    integer::N
    integer,allocatable,dimension(:)::A,B,T
    integer,allocatable,dimension(:,:)::DP
    integer::M,ans, siz1,siz2
    integer::i,j,S,k,x,y
    read*,n;m=2**n
    allocate(A(0:N-1),B(0:N-1),DP(0:M,0:N),T(0:N))
    read*,A(0:N-1)
    read*,B(0:N-1)
    T(0)=m-1
    do i=0,N-1
        T(i+1)=xor(T(i),2**i)
    end do
    do S=0,m-1
        do k=0,n-1
            dp(S,k)=10**9
        end do
    end do
    do k=0,N-1
        dp(2**k,k)=k
    end do
    
    do S=0,M-1
        siz1=0
        do i=0,n-1
            if (and(S,2**i)/=0)siz1=siz1+1
        end do
        do x=0,N-1
            siz2=0
            if (and(S,2**x)==0)cycle
            do i=0,N-1
                if (and(and(S,T(x+1)),2**i)/=0) siz2=siz2+1
            end do
            do y=0,N-1
                if (x==y.or.(and(S,2**y)==0))cycle
                if (f(y,siz1)<=f(x,siz1-1)) dp(S,x)=min(dp(S,x),dp(xor(S,2**x),y)+x+1-siz1+siz2)
            end do
        end do
    end do

    ans=10**5
    do i=0,N-1
        ans=min(ans,dp(m-1,i))        
    end do
    
    print"(I0)",merge(-1,ANS,ANS==10**5)
contains
integer function f(x,d)
    integer ::x,d
    f=merge(A(x),B(x),mod(X-d,2)==0)
end function
end program key2020_D