'use strict'

const permutation = ({ result = [], pre = [], post, n = post.length }) => {
  if (n > 0) {
    post.forEach((_, i) => {
      const rest = [...post];
      const elem = rest.splice(i, 1);
      permutation({ result, pre: [...pre, ...elem], post: rest, n: n - 1});
    });
  } else {
    result.push(pre);
  }
  return result;
};

function Main(input) {

  input = input.trim().split('\n')
  const N = parseInt(input[0])
  let P = input[1].split(' ').map(s => parseInt(s))
  let Q = input[2].split(' ').map(s => parseInt(s))

  const a = []
  for(let i = 0; i < N; ++i){ 
    a.push(i + 1)
  }

  const results = permutation({ post: a });

  const strP = P.join('')
  const strQ = Q.join('')

  let indexP = 0
  let indexQ = 0

  results.forEach((d, index) => {
    const s = d.join('')
    if(strP === s) indexP = index
    if(strQ === s) indexQ = index
  })
  console.log(Math.abs(indexP - indexQ));

  
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));