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