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)

//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//https://github.com/manta1130/Competitive_Programming_Template_Rust

#[macro_use]
mod input {

    use std;
    use std::io;

    const SPLIT_DELIMITER: char = ' ';

    #[macro_export]
    #[allow(unused_macros)]
    macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

    #[allow(dead_code)]
    pub fn input_line_str() -> String {
        let mut s = String::new();
        io::stdin().read_line(&mut s).unwrap();
        s.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn p<T>(t: T)
    where
        T: std::fmt::Display,
    {
        println!("{}", t);
    }

    #[allow(dead_code)]
    pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
    where
        T: std::str::FromStr,
    {
        let mut v: Vec<Vec<T>> = Vec::new();

        for _ in 0..line {
            let vec_line = input_vector();
            v.push(vec_line);
        }
        v
    }

    #[allow(dead_code)]
    pub fn input_vector<T>() -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v: Vec<T> = Vec::new();

        let s = input_line_str();
        let split_result = s.split(SPLIT_DELIMITER);
        for z in split_result {
            let buf = match z.parse() {
                Ok(r) => r,
                Err(_) => panic!("Parse Error"),
            };
            v.push(buf);
        }
        v
    }

    #[allow(dead_code)]
    pub fn input_vector_row<T>(n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::with_capacity(n);
        for _ in 0..n {
            let buf = match input_line_str().parse() {
                Ok(r) => r,
                Err(_) => panic!("Parse Error"),
            };
            v.push(buf);
        }
        v
    }

    pub trait ToCharVec {
        fn to_charvec(&self) -> Vec<char>;
    }

    impl ToCharVec for String {
        fn to_charvec(&self) -> Vec<char> {
            self.to_string().chars().collect::<Vec<_>>()
        }
    }
}

use input::*;

const INF: usize = 1_000_000_000;

fn main() {
    let n: usize;
    input!(n);
    let a = input_vector::<usize>();
    let b = input_vector::<usize>();
    let mut ans = 999999999;
    let mut flag = false;

    if n == 1 {
        p(0);
        return;
    }

    for k in 0..2_usize.pow(n as u32 - 1) {
        for i in 0..2_usize.pow(n as u32 - 1) {
            let mut aa = a.clone();
            let mut bb = b.clone();
            let mut cost = 0;
            for j in 0..n {
                if (i >> j) & 1 == 1 {
                    cost += 1;
                    let buf = aa[j];
                    aa[j] = bb[j + 1];
                    bb[j + 1] = buf;
                    let buf = bb[j];
                    bb[j] = aa[j + 1];
                    aa[j + 1] = buf;
                }
            }
            for j in 0..n {
                if (k >> j) & 1 == 1 {
                    cost += 1;
                    let buf = aa[j];
                    aa[j] = bb[j + 1];
                    bb[j + 1] = buf;
                    let buf = bb[j];
                    bb[j] = aa[j + 1];
                    aa[j + 1] = buf;
                }
            }
            let mut aa1 = vec![];
            let mut aa2 = vec![];

            for i in 0..n {
                if i % 2 == 0 {
                    aa1.push(aa[i]);
                } else {
                    aa2.push(aa[i]);
                }
            }

            let mut cost2 = 0;

            for i in 0..aa1.len() {
                for j in i + 1..aa1.len() {
                    if aa1[i] > aa1[j] {
                        cost2 += 3;
                    }
                }
            }

            for i in 0..aa2.len() {
                for j in i + 1..aa2.len() {
                    if aa2[i] > aa2[j] {
                        cost2 += 3;
                    }
                }
            }

            aa1.sort();
            aa2.sort();

            let mut ax = vec![];

            for i in 0..aa1.len() {
                ax.push(aa1[i]);
                if aa2.len() > i {
                    ax.push(aa2[i]);
                }
            }

            let mut flag2 = true;

            for i in 1..ax.len() {
                if ax[i - 1] > ax[i] {
                    flag2 = false;
                    break;
                }
            }

            if flag2 {
                flag = true;
                ans = std::cmp::min(cost + cost2, ans);
            }
        }
    }
    if !flag {
        println!("-1");
    } else {
        println!("{}", ans);
    }
}