'use strict'

var generatePermutation = function(perm, pre, post, n) {
  var elem, i, rest, len;
  if (n > 0)
    for (i = 0, len = post.length; i < len; ++i) {
      rest = post.slice(0);
      elem = rest.splice(i, 1);
      generatePermutation(perm, pre.concat(elem), rest, n - 1);
    }
  else
    perm.push(pre);
};

Array.prototype.permutation = function(n) {
  if (n == null) n = this.length;
  var perm = [];
  generatePermutation(perm, [], this, n);
  return perm;
};

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 = a.permutation()

  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"));