Problem:
Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0

Solution in JavaScript (node.js v5.12) (judge verdict: Accepted)

"use strict";
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill(0).map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill(0).map(x=>"+r+")";return eval(r);}
class permut{
  constructor(ary){
    var n=ary.length;
    this.array=ary;
    this.id=Array(n).fill().map((a,i)=>i);
    this.id2=Array(n).fill().map((a,i)=>i);
    this.moved=Array(n).fill(0);
    this.left=Array(n).fill(true);
    this.done=false;
  }
  static swap(self,i){
    var u=self.id2[i],v=self.id2[i-1],t=self.id[u];
    self.id2[i]=self.id2[i-1],self.id2[i-1]=u;
    self.id[u]=self.id[v],self.id[v]=t;
  }
  next(){
    if(this.done)return;
    var n=this.array.length,res=Array(n);
    for(var i=0;i<n;i++)res[i]=this.array[this.id2[i]];
    while(i--){
      if(this.moved[i]<i){
        this.moved[i]++;
        var m=this.left[i]?this.id[i]:this.id[i]+1;
        permut.swap(this,m);
        return res;
      }
      this.moved[i]=0,this.left[i]=!this.left[i];
    }
    this.done=true;
    return res;
  }
}

console.log(main());
function main(){
  var n = o.a();
  var p = +o.l().join("");
  var q = +o.l().join("");
  var pm = new permut(Array(n).fill().map((a,i)=>i+1));
  var ary = [];
  while(1){
    var t = pm.next();
    if(t === undefined)break;
    ary.push(+t.join(""));
  }
  ary.sort((a,b)=>a-b);
  var ans;
  for(var i = 0;;i++){
    if(p === ary[i]){
      ans = i;
      break;
    }
  }
  for(var i = 0;;i++){
    if(q === ary[i]){
      return Math.abs(i-ans);
    }
  }
}