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

let rec drop n = function
  | [] -> []
  | (_ :: t) as l ->
      if n = 0
      then l
      else drop (n - 1) t

let sort_count ( <= ) xs =
  let rec merge_count acc xs ys n cnt =
    match xs, ys with
    | _, [] -> cnt, List.rev_append acc xs
    | [], _ -> cnt, List.rev_append acc ys
    | x :: xs', y :: ys' ->
        if x <= y
        then merge_count (x :: acc) xs' ys (n - 1) cnt
        else merge_count (y :: acc) xs ys' n (n + cnt) in
  let rec sort_count xs = function
    | 0 -> 0, []
    | 1 -> 0, [List.hd xs]
    | n ->
        let c, ys = sort_count xs (n / 2) in
        let d, zs = sort_count (drop (n / 2) xs) ((n + 1) / 2) in
        merge_count [] ys zs (n / 2) (c + d) in
  sort_count xs (List.length xs)

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 f i = if s land (1 lsl i) = 0 then as_.(i) else bs.(i) in
      let (inv, xs) =
        sort_count (fun i j -> f i <= f j) @@
        Array.to_list @@
        Array.init n @@ fun i -> i in
      match
        List.mapi (fun i j ->
          (f j, (i mod 2 = j mod 2) = (s land (1 lsl j) = 0))) xs
      with
      | [] -> max_int
      | [(_, true)] -> inv
      | [(_, false)] -> max_int
      | (x, b) :: (x', b') :: xbs ->
          try
            let (x, b, x', b', inv) =
              List.fold_left (fun (x, b, x', b', inv) (x'', b'') ->
                match b, x = x', b',    x' = x'', b'' with
                | true,  _,      _,     _,        _ ->     (x', b', x'', b'', inv)
                | false, false,  _,     _,        _ ->     raise Not_found
                | false, true,   false, _,        _ ->     (x', true, x'', b'', inv + 1)
                | false, true,   true,  false,    _ ->     raise Not_found
                | false, true,   true,  true,     false -> raise Not_found
                | false, true,   true,  true,     true ->  (x', true, x'', false, inv + 2))
              (x, b, x', b', inv) xbs in
            if b && b'
            then inv
            else if not b && not b' && x = x'
            then inv + 1
            else max_int
          with Not_found -> max_int in
  Printf.printf "%d\n" @@ if ans = max_int then -1 else ans