#[allow(unused_macros)]
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(unused_macros)]
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(unused_macros)]
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, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

fn main() {
    input! {
        n: usize,
        a: [i64; n],
        b: [i64; n],
    }

    let mut ans = -1;

    use std::io::Write;

    for bp in 0..1 << n {
        let mut e = vec![];
        let mut o = vec![];

        for i in 0..n {
            if bp & (1 << i) != 0 {
                if (i + 1) % 2 == 0 {
                    e.push((b[i], i));
                } else {
                    o.push((b[i], i));
                }
            } else {
                if i % 2 == 0 {
                    e.push((a[i], i));
                } else {
                    o.push((a[i], i));
                }
            }
        }

        let ok = if n % 2 == 0 { e.len() == o.len() } else { e.len() == o.len() + 1 };
        if !ok {
            continue;
        }

        e.sort();
        o.sort();

        // writeln!(&mut std::io::stderr(), "e:{:?}, o:{:?}", &e, &o);

        let mut ok = true;

        for i in 0..n-1 {
            ok &= if i % 2 == 0 {
                e[i/2].0 <= o[i/2].0
            } else {
                o[i/2].0 <= e[i/2+1].0
            };

            if !ok {
                break;
            }
        }

        if !ok {
            continue;
        }

        let mut sum = 0;

        let mut ix = vec![];

        // for i in 0..n {
        //     if i % 2 == 0 {
        //         sum += (e[i/2].1 as i64 - i as i64).abs();
        //     } else {
        //         sum += (o[i/2].1 as i64 - i as i64).abs();
        //     }
        // }

        for i in 0..n {
            if i % 2 == 0 {
                ix.push(e[i/2].1);
            } else {
                ix.push(o[i/2].1);
            }
        }

        // writeln!(&mut std::io::stderr(), "ix:{:?}, sum:{}", &ix, sum);

        for i in 0..n {
            if ix[i] == i {
                continue;
            }

            for j in i+1..n {
                if ix[j] == i {
                    for k in (i+1..j+1).rev() {
                        ix[k] = ix[k-1];
                    }
                    ix[i] = i;
                    sum += (j-i) as i64;
                    break;
                }
            }
        }

        // writeln!(&mut std::io::stderr(), "ix:{:?}, sum:{}", &ix, sum);

        // assert!(sum % 2 == 0);

        // let sum = sum / 2;

        // writeln!(&mut std::io::stderr(), "e:{:?}, o:{:?}, sum:{}", &e, &o, sum);

        if ans == -1 || sum < ans {
            ans = sum;
        }
    }

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


Measured memory_bytes: 4352