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: Accepted)

//! ----------------------------------------------
//! Framework <https://github.com/vain0x/procon>
//!
//! See the bottom of file for solution.
//! ----------------------------------------------

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Display, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

/// Print values to standard error if debug mode.
#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", e, $e).unwrap()
        })*
    };
}

/// Read from standard input and parse each word.
/// - `read!(T, U, ..)` parses a line as a tuple of words.
/// - `read![[T]]` parses a line as an array of words.
/// - `read![..; N]` parses `N` lines, using `read!(..)` repeatedly.
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

/// Read a line from standard input.
#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();

    #[allow(deprecated)]
    buf.trim_right().to_owned()
}

// -----------------------------------------------
// Solution
// -----------------------------------------------

/*

5
3 1 4 1 5
3 1 4 1 5
3

5
3 1 4 1 5
9 2 6 5 3
-1

5
1 4 1 4 2
2 2 3 6 6
3

*/

fn neg(x: &mut bool) {
    *x = !*x;
}

fn main() {
    let N = read!(usize);
    let A = read![[i64]];
    let B = read![[i64]];

    let mut numbers = vec![Default::default(); N];
    let mut cards = vec![Default::default(); N];
    let mut parity = vec![Default::default(); N];
    let mut min_cost = std::i64::MAX;

    // 各カードの移動回数の偶奇を決め打ちすることで結果の数列を決定する。
    for s in 0..1 << N {
        let flip = |i: usize| (s & (1 << i)) != 0;

        for i in 0..N {
            let c = if flip(i) { B[i] } else { A[i] };
            cards[i] = (c, i);
            numbers[i] = c;
            parity[i] = false;
        }

        numbers.sort();

        // 選択ソートによりカードの表面の数字の列を numbers に一致させる。

        let mut l = 0;
        let mut cost = 0;
        let mut ok = true;

        while l < N {
            // 左端の数値に一致する最初のカードを見つける。ただし偶奇が壊れるものは除く。
            let i = match (l..N)
                .filter(|&i| {
                    cards[i].0 == numbers[l]
                        && parity[cards[i].1] ^ ((i - l) % 2 != 0) == flip(cards[i].1)
                })
                .next()
            {
                None => {
                    ok = false;
                    break;
                }
                Some(i) => i,
            };

            for j in (l..i).rev() {
                cards.swap(j, j + 1);
                neg(&mut parity[cards[j].1]);
                neg(&mut parity[cards[j + 1].1]);
                cost += 1;
            }

            debug_assert_eq!(cards[l].0, numbers[l]);
            if parity[cards[l].1] != flip(cards[l].1) || cost > min_cost {
                ok = false;
                break;
            }
            l += 1;
        }

        if ok {
            min_cost = min(min_cost, cost);
        }
    }

    if min_cost == std::i64::MAX {
        println!("-1")
    } else {
        println!("{}", min_cost)
    }
}