let rec popcnt acc = function
  | 0 -> acc
  | n -> popcnt (acc + (acc land 1)) (n lsr 1)
let popcnt = popcnt 0

module SegTree = struct
  module type SemiGroup = sig
    type t
    val op : t -> t -> t
  end

  module Make (S : SemiGroup) : sig
    type t
  type elt

  (* 与えられたリストの要素からなるセグ木を作る *)
  val of_list : elt list -> t
  (* f 0, ... f (n - 1)のn要素からなるセグ木を作る *)
  val init : int -> (int -> elt) -> t
  (*
   * update i f t
   * i番目の要素x_iをf x_iに変更したセグ木を作る
   *)
  val update : int -> (elt -> elt) -> t -> t
  (*
   * query l r t
   * 添字が[l, r)の要素を半群の演算子で畳み込んだ値を求める
   *)
  val query : int -> int -> t -> elt
  end with type elt = S.t = struct
    type elt = S.t

  type body =
    | Leaf of elt
    | Node of elt * body * body
  type t = { size : int; body : body }

  (* セグ木の要素をどのように左右に分配するか
     正整数nについて lsize n + rsize n = n が成り立たなくてはならない *)
  let lsize n = n lsr 1
  let rsize n = (n + 1) lsr 1

  (* セグ木の保持する要素を半群の演算子で畳み込んだもの *)
  let data = function
    | Leaf x
    | Node (x, _, _) -> x

  let mknode l r = Node (S.op (data l) (data r), l, r)

  let rec of_list l = function
    | 1 -> Leaf (List.hd l), List.tl l
    | n ->
        let t1, l = of_list l (lsize n) in
        let t2, l = of_list l (rsize n) in
        mknode t1 t2, l
  let of_list l =
    let n = List.length l in
    assert (0 < n);
    { size = n; body = fst (of_list l n) }

  let rec init i f = function
    | 1 -> Leaf (f i)
    | n -> mknode (init i f (lsize n)) (init (i + lsize n) f (rsize n))
  let init n f =
    assert (1 <= n);
    { size = n; body = init 0 f n }

  let rec update n i f = function
    | Leaf x -> Leaf (f x)
    | Node (_, l, r) ->
        if i < lsize n
        then mknode (update (lsize n) i f l) r
        else mknode l (update (rsize n) (i - lsize n) f r)
  let update i f t =
    assert (0 <= i && i < t.size);
    { t with body = update t.size i f t.body }

  let rec query n l r = function
    | Leaf x -> x
    | Node (x, left, right) ->
        if l = 0 && r = n then x
        else if r <= lsize n then query (lsize n) l r left
        else if lsize n <= l then query (rsize n) (l - lsize n) (r - lsize n) right
        else S.op (query (lsize n) l (lsize n) left) (query (rsize n) 0 (r - lsize n) right)
  let query l r t =
    assert (0 <= l && l < r && r <= t.size);
    query t.size l r t.body
  end
end

module SumSegTree = SegTree.Make (struct
  type t = int
  let op = ( + )
end)

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let bs = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun b -> b in
  let ans =
    Array.fold_left min max_int @@
    Array.init (1 lsl n) @@ fun s ->
      if popcnt s mod 2 = 1 then max_int else
      let (inv, diff, _) =
        Array.fold_left (fun (inv, diff, same) (x, b) ->
          let d = SumSegTree.query (x + 1) 52 diff + SumSegTree.query (x + 1) 52 same in
          if (d mod 2 = 0) = b
          then (inv + d, diff, SumSegTree.update x succ same)
          else (inv + d, SumSegTree.update x succ diff, same))
        (0, SumSegTree.init 53 (fun _ -> 0), SumSegTree.init 53 (fun _ -> 0)) @@
        Array.init n @@ fun i -> if (s land (1 lsl i)) = 0 then (as_.(i), true) else (bs.(i), false) in
      try
        Array.fold_left ( + ) inv @@
        Array.init 51 @@ fun i ->
          let x = SumSegTree.query i (i + 1) diff in
          if x mod 2 = 1
          then raise Not_found
          else x / 2
      with Not_found -> max_int in
  Printf.printf "%d\n" @@ if ans = max_int then -1 else ans