program swap_and_flip
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, a(18) = 0, b(18) = 0, i, j, k, x, y, u, v
  integer, allocatable :: dp(:,:)
  read(*,*) n
  read(*,*) a(1:n)
  read(*,*) b(1:n)
  u = lshift(1,n)-1
  allocate(dp(0:u,0:50))
  dp = inf
  dp(0,0) = 0
  do k = 0, u
    do i = 1, 50
      dp(k,i) = min(dp(k,i),dp(k,i-1))
    end do
    x = popcnt(k)
    do i = 1, n
      if (btest(k,i-1)) cycle
      if (modulo(x-i+1,2) == 1) then
        v = b(i)
      else
        v = a(i)
      end if
      y = popcnt(rshift(k,i-1))
      j = ibset(k,i-1)
      dp(j,v) = min(dp(j,v),dp(k,v)+y)
    end do
  end do
  x = minval(dp(u,:))
  if (x == inf) x = -1
  write(*,'(i0)') x
end program swap_and_flip