import Data.Bool
import Data.List
import Data.Bits
main=interact$show.solve.map(map read.words).lines
solve[[n],as,bs]=(\m->if m==inf then -1 else m).foldr f inf$ds where
  ds=filter(\d->popCount d==n`div`2)[0..shift 1 n-1::Int]
  f d m=if or.(zipWith(<)=<<tail)$map fst r then m else min m c where
    c=sum$zipWith(\j i->length$filter(>i)$take j is)[0..]is where is=map snd r
    r=(\(e,o)->h(sort e)(sort o)).foldr(g d)([],[]).zip[0..n-1]$zip as bs
g d(i,(a,b))(e,o)=if testBit d i then(e,v:o)else(v:e,o)where
  v=(bool b a$testBit d i==(i.&.1/=0),i)
h(a:as)bs=a:h bs as
h _ _=[]
inf=maxBound::Int