'use strict';
function* permutation(a,b,n){
  if ( n === 0 ) return yield b;
  for ( let i = 0; i < n; i++ ) {
    const _a = [...a];
    const c = _a.splice(i,1)[0];
    yield* permutation(_a, [...b,c], n-1);
  }
}
console.log(function(args){
  const _tmp = args.trim().split('\n');
  const N = _tmp[0]|0;
  const P = _tmp[1].split(' ').map(v=>v|0);
  const Q = _tmp[2].split(' ').map(v=>v|0);
  let a = 0, b = 0, c = 0;
  const O = Array(N).fill(0).map((a,i)=>i+1);
  for ( let o of permutation(O,[],N) ) {
    c++;
    if ( o.every((v,i)=>v===P[i]) ) a = c;
    if ( o.every((v,i)=>v===Q[i]) ) b = c;
    if ( a && b ) break;
  }
  return Math.abs(a-b).toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
