program swap_and_flip
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, a(0:17) = 0, b(0:17) = 0, i, j, k, x, u, v
  integer, allocatable :: dp(:,:)
  read(*,*) n
  read(*,*) a(0:n-1)
  read(*,*) b(0:n-1)
  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 = 0, n-1
      if (btest(k,i)) cycle
      v = merge(b(i),a(i),modulo(x-i,2) == 1)
      j = ibset(k,i)
      dp(j,v) = min(dp(j,v),dp(k,v)+popcnt(rshift(k,i)))
    end do
  end do
  x = minval(dp(u,:))
  write(*,'(i0)') merge(-1,x,x == inf)
end program swap_and_flip