Problem:
Score : 700 points

Problem Statement

We have N cards numbered 1, 2, ..., N.
Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side.
Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.

Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.

Choose an integer i (1 \leq i \leq N - 1).
Swap the i-th and (i+1)-th cards from the left, then flip these two cards.

Constraints

1 \leq N \leq 18

1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If it is impossible to have a non-decreasing sequence, print -1.
If it is possible, print the minimum number of operations required to achieve it.

Sample Input 1

3
3 4 3
3 2 3

Sample Output 1

1

By doing the operation once with i = 1, we have a sequence [2, 3, 3] facing up, which is non-decreasing.

Sample Input 2

2
2 1
1 2

Sample Output 2

-1

After any number of operations, we have the sequence [2, 1] facing up, which is not non-decreasing.

Sample Input 3

4
1 2 3 4
5 6 7 8

Sample Output 3

0

No operation may be required.

Sample Input 4

5
28 15 22 43 31
20 22 43 33 32

Sample Output 4

-1

Sample Input 5

5
4 46 6 38 43
33 15 18 27 37

Sample Output 5

3

Solution in OCaml (4.02.3) (judge verdict: Wrong Answer)

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