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(dead_code)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(dead_code)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

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

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

macro_rules! order_by {
    ($($x:tt)*) => {
        |l, r| {
            order_by_inner!(l, r, $($x)*)
        }
    }
}
macro_rules! order_by_inner {
    () => {};
    ($l:ident) => {std::cmp::Ordering::Equal};
    ($l:ident , ) => {std::cmp::Ordering::Equal};
    ($l:ident , $r:ident) => {std::cmp::Ordering::Equal};
    ($l:ident , $r:ident , ) => {std::cmp::Ordering::Equal};

    // last
    ($l:ident , $r:ident , $x:tt asc) => {
        $l.$x.cmp(&$r.$x)
    };
    ($l:ident , $r:ident , $x:tt desc) => {
        $l.$x.cmp(&$r.$x).reverse()
    };
    ($l:ident , $r:ident , $x:tt) => {
        order_by_inner!($l, $r, $x asc)
    };

    // mid
    ($l:ident , $r:ident , $x:tt asc , $($p:tt)+) => {
        match $l.$x.cmp(&$r.$x) {
            std::cmp::Ordering::Equal => {
                order_by_inner!($l, $r, $($p)+)
            },
            other => other
        }
    };
    ($l:ident , $r:ident , $x:tt desc , $($p:tt)+) => {
        match $l.$x.cmp(&$r.$x).reverse() {
            std::cmp::Ordering::Equal => {
                order_by_inner!($l, $r, $($p)+)
            },
            other => other
        }
    };
    ($l:ident , $r:ident , $x:tt , $($p:tt)+) => {
        order_by_inner!($l, $r, $x asc, $($p)+)
    };
}
fn main() {
    input! {
        n: usize,
        aa: [usize; n],
        bb: [usize; n],
    }

    let mut ans = -1;
    // 0b0000 A3, A2, A1, A0
    // 0b0101 A3, B2, A1, B0
    for bit in 0b0 .. 0b1 << n {
        let mut even_pos = vec![];
        let mut odd_pos = vec![];

        // use cards
        for i in 0..n {
            let i_is_even = i % 2 == 0;
            if bit & (0b1 << i) == 0 {
                // use A
                if i_is_even {
                    even_pos.push((aa[i], i));
                } else {
                    odd_pos.push((aa[i], i));
                }
            } else {
                // use B
                if i_is_even {
                    odd_pos.push((bb[i], i));
                } else {
                    even_pos.push((bb[i], i));
                }
            }
        }

        // cannot create
        if even_pos.len() != odd_pos.len() && even_pos.len() != odd_pos.len() + 1 {
            continue;
        }

        even_pos.sort();
        odd_pos.sort();

        let result = {
            let mut v = vec![];
            let mut ei = even_pos.into_iter();
            let mut oi = odd_pos.into_iter();
            loop {
                match (ei.next(), oi.next()) {
                    (Some(e), Some(o)) => {
                        v.push(e);
                        v.push(o);
                    },
                    (Some(e), None) => {
                        v.push(e);
                    },
                    (None, Some(o)) => {
                        v.push(o);
                    },
                    (None, None) => break
                }
            }
            v
        };
        if !sorted(&result) {
            continue;
        }

        let n = bubble_cnt(&result, &|&(_, index)| index) as isize;
        if ans == -1 || ans > n {
            ans = n;
        }
    }

    println!("{}", ans);
}

fn sorted<T>(v: &[T]) -> bool
where
    T: Ord
{
    v.windows(2).all(|w| w[0] <= w[1])
}

fn bubble_cnt<T, F>(v: &Vec<T>, f: &F) -> usize
where
    F: Fn(&T) -> usize
{
    let mut indices = v.iter().map(f).collect::<Vec<_>>();
    let mut cnt = 0;
    let mut last_change = indices.len() - 1;
    while last_change > 1 {
        let mut tmp = 0;
        for i in 0..last_change {
            if indices[i] > indices[i + 1] {
                cnt += 1;
                indices.swap(i, i + 1);
                tmp = i + 1;
            }
        }
        last_change = tmp;
    }
    cnt
}