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 Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::cmp;
use std::io::Write;

// Input macro from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
  (source = $s:expr, $($r:tt)*) => {
    let mut iter = $s.split_whitespace();
    input_inner!{iter, $($r)*}
  };
  ($($r:tt)*) => {
    let s = {
      use std::io::Read;
      let mut s = String::new();
      std::io::stdin().read_to_string(&mut s).unwrap();
      s
    };
    let mut iter = s.split_whitespace();
    input_inner!{iter, $($r)*}
  };
}

macro_rules! input_inner {
  ($iter:expr) => {};
  ($iter:expr, ) => {};

  ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
    let $var = read_value!($iter, $t);
    input_inner!{$iter $($r)*}
  };
}

macro_rules! read_value {
  ($iter:expr, ( $($t:tt),* )) => {
    ( $(read_value!($iter, $t)),* )
  };

  ($iter:expr, [ $t:tt ; $len:expr ]) => {
    (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
  };

  ($iter:expr, chars) => {
    read_value!($iter, String).chars().collect::<Vec<char>>()
  };

  ($iter:expr, usize1) => {
    read_value!($iter, usize) - 1
  };

  ($iter:expr, [ $t:tt ]) => {{
    let len = read_value!($iter, usize);
    (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
  }};

  ($iter:expr, $t:ty) => {
    $iter.next().unwrap().parse::<$t>().expect("Parse error")
  };
}

// 1-indexed
// example: Bit::new(n, |x, y| *x += *y, 0)
struct Bit<T, F> {
  n: usize,
  dat: Vec<T>,
  op: F,
  unit: T,
}

impl<T: Clone, F: Fn(&mut T, &T)> Bit<T, F> {
  #[allow(dead_code)]
  fn new(n: usize, op: F, unit: T) -> Self {
      Bit {
          n: n,
          dat: vec![unit.clone(); n + 1],
          op: op,
          unit: unit,
      }
  }
  #[allow(dead_code)]
  fn from_vec(mut v: Vec<T>, op: F, unit: T) -> Self {
      let n = v.len();
      let mut dat = vec![unit.clone()];
      dat.append(&mut v);
      for i in 1..n {
          let j = i as i32;
          let b = (j & -j) as usize;
          let x = dat[i].clone();
          op(&mut dat[i + b], &x);
      }
      Bit {
          n: n,
          dat: dat,
          op: op,
          unit: unit,
      }
  }
  fn operate(&mut self, k: usize, a: &T) {
      let mut k = k;
      while k <= self.n {
          (self.op)(&mut self.dat[k], &a);
          let l = k as i32;
          k += (l & -l) as usize;
      }
  }
  fn accum(&self, k: usize) -> T {
      let mut k = k;
      let mut sum = self.unit.clone();
      while k > 0 {
          (self.op)(&mut sum, &self.dat[k]);
          let l = k as i32;
          k -= (l & -l) as usize;
      }
      sum
  }
}

const INF: i64 = 1 << 60;

fn main() {
  let out = std::io::stdout();
  let mut out = std::io::BufWriter::new(out.lock());
  macro_rules! puts {
    ($($format:tt)*) => (write!(out,$($format)*).unwrap());
  }
  input! {
    n: usize,
    a: [i32; n],
    b: [i32; n],
  }
  let mut ans = INF;
  for pat in 0 .. (1 << n) {
    let mut flip = [0, 0];
    for i in 0 .. n {
      if (pat >> i) & 1 != 0 {
        flip[i % 2] += 1;
      }
    }
    if flip[0] != flip[1] {
      continue;
    }
    let mut nums = vec![vec![]; 2];
    for i in 0 .. n {
      let flipped = (pat >> i) & 1 != 0;
      let v = if flipped {
        b[i]
      } else {
        a[i]
      };
      if flipped {
        nums[(i + 1) % 2].push((v, i));
      } else {
        nums[i % 2].push((v, i));
      }
    }
    nums[0].sort();
    nums[1].sort();
    let mut sorted = true;
    for i in 0 .. nums[1].len() {
      if nums[0][i] > nums[1][i] {
        sorted = false;
        break;
      }
    }
    for i in 1 .. nums[0].len() {
      if nums[1][i-1] > nums[0][i] {
        sorted = false;
        break;
      }
    }
    if ! sorted {
      continue;
    }
    let mut bit = Bit::new(n, |x, y| *x += *y, 0);
    let mut swap = 0;
    for i in (0 .. n).rev() {
      let index = nums[i % 2][i / 2].1 + 1;
      swap += bit.accum(index);
      bit.operate(index, &1);
    }
    ans = cmp::min(ans, swap);
  }

  puts!("{}\n", if ans == INF { -1 } else { ans });
}