'use strict';
function main(input) {
  const args = input.split('\n');
  const N = parseInt(args[0]),
        P = args[1].split(' '),
        Q = args[2].split(' ');
  var orderOfP = 0,
      orderOfQ = 0;
  var numbersOfP = [],
      numbersOfQ = [];
  for (let i = 1; i <= N; ++i) {
    numbersOfP.push(i);
    numbersOfQ.push(i);
  }
  for (let i = 0; i < N; ++i) {
    var p = parseInt(P[i]),
        q = parseInt(Q[i]);
    orderOfP += numbersOfP.indexOf(p) * factorial(N-1-i);
    orderOfQ += numbersOfQ.indexOf(q) * factorial(N-1-i);
    numbersOfP = numbersOfP.filter(number => number !== p);
    numbersOfQ = numbersOfQ.filter(number => number !== q);
  }
  console.log(Math.abs(orderOfQ - orderOfP));
}

function factorial(n) {
  var ans = 1;
  if (n > 1) {
    for (let i = 2; i <= n; ++i) {
      ans *= i;
    }
  }
  return ans;
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));