use std::io::stdin;

fn main() {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    let n: usize = buf.trim().parse().unwrap();

    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    let line = buf.split_whitespace();
    let mut p: Vec<u32> = Vec::new();
    for iter in line{
        p.push(iter.parse().unwrap());
    }

    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    let line = buf.split_whitespace();
    let mut q: Vec<u32> = Vec::new();
    for iter in line {
        q.push(iter.parse().unwrap());
    }

    println!("{}", solve(n, p, q))
}

fn solve(n: usize, p: Vec<u32>, q: Vec<u32>) -> i32{
    let mut num_list = Vec::new();
    for num in 1..n+1{
        num_list.push(num as u32);
    }

    let mut pattern = Vec::new();
    loop {
        pattern.push(num_list.clone());
        if !num_list.next_permutation() {
            break;
        }
    }

    let p_num;
    let mut index = 0;
    loop {
        if pattern[index] == p {
            p_num = index as i32;
            break;
        }
        index += 1;
    }

    let q_num;
    let mut index = 0;
    loop {
        if pattern[index] == q {
            q_num = index as i32;
            break;
        }
        index += 1;
    }

    match p_num - q_num{
        result if result < 0 => -result,
        result if result >= 0 => result,
        _ => unreachable!()
    }
}

// copy from https://github.com/bluss/permutohedron/blob/master/src/lexical.rs
pub trait LexicalPermutation {
    /// Return `true` if the slice was permuted, `false` if it is already
    /// at the last ordered permutation.
    fn next_permutation(&mut self) -> bool;
    /// Return `true` if the slice was permuted, `false` if it is already
    /// at the first ordered permutation.
    fn prev_permutation(&mut self) -> bool;
}

impl<T> LexicalPermutation for [T] where T: Ord {
    /// Original author in Rust: Thomas Backman <serenity@exscape.org>
    fn next_permutation(&mut self) -> bool {
        // These cases only have 1 permutation each, so we can't do anything.
        if self.len() < 2 { return false; }

        // Step 1: Identify the longest, rightmost weakly decreasing part of the vector
        let mut i = self.len() - 1;
        while i > 0 && self[i-1] >= self[i] {
            i -= 1;
        }

        // If that is the entire vector, this is the last-ordered permutation.
        if i == 0 {
            return false;
        }

        // Step 2: Find the rightmost element larger than the pivot (i-1)
        let mut j = self.len() - 1;
        while j >= i && self[j] <= self[i-1]  {
            j -= 1;
        }

        // Step 3: Swap that element with the pivot
        self.swap(j, i-1);

        // Step 4: Reverse the (previously) weakly decreasing part
        self[i..].reverse();

        true
    }

    fn prev_permutation(&mut self) -> bool {
        // These cases only have 1 permutation each, so we can't do anything.
        if self.len() < 2 { return false; }

        // Step 1: Identify the longest, rightmost weakly increasing part of the vector
        let mut i = self.len() - 1;
        while i > 0 && self[i-1] <= self[i] {
            i -= 1;
        }

        // If that is the entire vector, this is the first-ordered permutation.
        if i == 0 {
            return false;
        }

        // Step 2: Reverse the weakly increasing part
        self[i..].reverse();

        // Step 3: Find the rightmost element equal to or bigger than the pivot (i-1)
        let mut j = self.len() - 1;
        while j >= i && self[j-1] < self[i-1]  {
            j -= 1;
        }

        // Step 4: Swap that element with the pivot
        self.swap(i-1, j);

        true
    }

}

Measured memory_bytes: 6396