#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <climits>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vec> vec2;
typedef map<ll,ll> MPll;
typedef set<ll> setl;

const ll INF = 1ll << 60;
const ld EPS = 1e-10;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const ll MOD = 1000000007;

//for文
#define FOR(i,a,b) for(ll i = (ll)a;i < (ll)b;i++)
#define FORE(i,a,b) for(ll i = (ll) a;i <= (ll) b;i++ )
#define REP(i,size)  for(ll i =(ll)0;i<size;i++)
#define REPE(i,size)  for(ll i =(ll)0;i<=size;i++)
#define REPR(i,size)  for(ll i =(ll)size;i>=0;i--)
#define FOREACH(it, vec) for (auto it = vec.begin(); it != vec.end(); it++)

//ソート
#define ALL(vec) (vec).begin(),(vec).end()
#define SORT(vec) sort(ALL(vec))
#define SORTA(arr) sort(arr,arr + (sizeof(arr) / sizeof(ll)))
#define INVSORT(vec) sort((vec).rbegin(),(vec).rend())
#define REV(vec) reverse(ALL(vec))
#define REVA(arr) reverse(arr,arr + (sizeof(arr) / sizeof(ll)))
#define INVSORTA(arr) sort(arr,arr + (sizeof(arr) / sizeof(ll))),REVA(arr)

//最大値最小値
#define MAX(vec) *max_element(ALL(vec))
#define UNIQ(vec) SORT(vec);vec.erase(unique(ALL(vec)),vec.end())
#define MIN(vec) *min_element(ALL(vec))

//出力
#define printl(a) cout << a << "\n"
#define print(a) cout << a
#define OUT(a) printf("%lld\n",a)
#define OUTA(array) REP(i,sizeof(array)/sizeof(ll)) printf("%lld\n",array[i])
#define OUTV(vec) REP(i,vec.size()) printf("%lld\n",vec[i])
#define SP printf(" ")

//入力
#define IN(x) scanf("%lld",&x)
#define INV(vec) REP(i,vec.size()) scanf("%lld",&vec[i])
#define INA(array) REP(i,sizeof(array)/sizeof(ll)) scanf("%lld",array + i)
#define INS(x) cin >> x
#define INCH(x) scanf(" %c",&x)

//型
#define P pair
#define vp vector<P>
#define F first
#define S second

//その他
#define PB push_back
#define MP make_pair
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define INFI(a) memset(a,INF,sizeof(a))
#define MEM(a,b) memset(a,(b),sizeof(a))

//関数
template<class T> inline void amax(T &a,const T &b) { if(a < b) a = b; }
template<class T> inline void amin(T &a, const T &b) { if(a > b) a = b; }
/*
struct structure{
    ll num1,num2,string s;
    bool operator<(const rest &another) const{
        return this->s < another.s;
    }
    structure(ll a,ll b,string s){
        this->num1 = a,this->num2=b,this->s=s;
    }
};a
*/
//特殊
//#define int ll
#define _CRT_SECURE_NO_WARNINGS

#define DEBUG

#ifdef DEBUG
#define debugl(x) cerr << #x << ":" << x<<"\n"
#define debug(x)  cerr << x<<endl;
#define  debugV(V) REP(i,V.size()){cerr << i << ":" << V[i]<<endl;}
#define  debugA(A) REP(i,sizeof(A)/sizeof(ll)){cerr << i << ":" << V[i]<<endl;}
#else
#define debug(x)
#define debugV(x)
#define debugA(x)
#define debugl(x)
#endif

#define ZERO(a) memset(a,0,sizeof(a))
#define ALL(vec) (vec).begin(),(vec).end()
#define SORT(vec) sort(ALL(vec))

#include <cstdint>




struct Edge{
    ll from,to,cost;
    Edge(ll to,ll from, ll cost)/*: from(from),to(to),cost(cost)*/{
        this->to = to;
        this->from = from;
        this ->cost = cost;
    }

    bool operator <(const Edge &another) const{
        if (this->cost != another.cost) return this->cost < another.cost;
        if (this->to != another.to) return this->to < another.to;
        return this ->from < another.from;
    }

};


ll rep_pow(ll x,ll n){
    if (n == 0) return 1;
    if (n%2 == 0){
        ll ans = rep_pow(x,n/2);
        return ans * ans % MOD;
    }

    return x * rep_pow(x,n/2);
}


ll rep_pow2(ll x,ll n){
    ll ans = 1;
    while(n >0){
        if (n & 1){
            ans = ans * x % MOD;
        }

        x = x * x % MOD;
        n = n >> 1;
    }

    return ans;

}


template <typename T>
struct UnionFindTree{

    vector<T> par;

    UnionFindTree(T n) : par(n+1){
        for (int i = 1; i <= n; ++i) {
            par[i] = i;
        }
    }

    T root(T x){
        if(par[x] == x) return x;
        else  return par[x] = root(par[x]);
    }

    void unite(T x,T y){
        if(!same(x,y)) par[root(x)] = root(par[y]);
    }

    T same(T x,T y){
        return root(x) == root(y);
    }

};

int const inf = INT_MAX;
template <typename T>
struct SegmentTree {
    T n;
    vector<T> node;

    SegmentTree(T k)  {
        n = 0; while(n < k) n *= 2;
        node.resize(n, inf);
    }

    void update(T i, T val) {
        i += n-1;
        node[i] = val;
        while(i > 0) {
            i = (i - 1) / 2;
            node[i] = min(node[2*i+1], node[2*i+2]);
        }
    }

    T find(T a, T b, T k, T l, T r) {
        if(r == -1) r = n;
        if(r <= a or b <= l) return inf;
        if(a <= l and r <= b) return node[k];
        T h = (l + r) /2 ;
        return min(find(a, b, 2*k+1, l, h),find(a, b, 2*k+2, h, r));
    }
};


ll fermat_combination(ll n,ll k,vec fac,vec inv){
    ll ans = fac[n] * inv[k] % MOD * inv[n-k] % MOD;
    return  ans;
}




long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}


// An efficient C++ program to find top K elements
// from two arrays.
using namespace std;

// Function prints k maximum possible combinations
void KMaxCombinations(vector<ll>& A,
                      vector<ll>& B, int K)
{
    // sort both arrays A and B
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());

    int N = A.size();

    // Max heap which contains tuple of the format
    // (sum, (i, j)) i and j are the indices
    // of the elements from array A
    // and array B which make up the sum.
    priority_queue<pair<int, pair<int, int> > > pq;

    // my_set is used to store the indices of
    // the pair(i, j) we use my_set to make sure
    // the indices doe not repeat inside max heap.
    set<pair<int, int> > my_set;

    // initialize the heap with the maximum sum
    // combination ie (A[N - 1] + B[N - 1])
    // and also push indices (N - 1, N - 1) along
    // with sum.
    pq.push(make_pair(A[N - 1] * B[N - 1],
                      make_pair(N-1, N-1)));

    my_set.insert(make_pair(N - 1, N - 1));

    // iterate upto K
    for (int count=0; count<K; count++) {

        // tuple format (sum, (i, j)).
        pair<int, pair<int, int> > temp = pq.top();
        pq.pop();

        cout << temp.first << endl;

        int i = temp.second.first;
        int j = temp.second.second;

        int multi = A[i - 1] * B[j];

        // insert (A[i - 1] + B[j], (i - 1, j))
        // into max heap.
        pair<int, int> temp1 = make_pair(i - 1, j);

        // insert only if the pair (i - 1, j) is
        // not already present inside the map i.e.
        // no repeating pair should be present inside
        // the heap.
        if (my_set.find(temp1) == my_set.end()) {
            pq.push(make_pair(multi, temp1));
            my_set.insert(temp1);
        }

        // insert (A[i] + B[j - 1], (i, j - 1))
        // into max heap.
        multi = A[i] * B[j - 1];
        temp1 = make_pair(i, j - 1);

        // insert only if the pair (i, j - 1)
        // is not present inside the heap.
        if (my_set.find(temp1) == my_set.end()) {
            pq.push(make_pair(multi, temp1));
            my_set.insert(temp1);
        }
    }
}

// Driver Code.
int main()
{
    int N;
    cin >> N;

    vector<int> v(N);
    vector<int> p(N);
    vector<int> q(N);
    FOR(i,0,N) cin >> p[i];
    FOR(i,0,N) cin >> q[i];
    iota(v.begin(), v.end(), 1);       // v に 1, 2, ... N を設定
    int a,b,ans;
    ans = 1;
    do {
        bool p_falg = true;
        bool q_falg  = true;
        int i = 0;
        for(auto x : v) {
            if(x != p[i]) p_falg = false;
            if(x != q[i]) q_falg = false;
            i++;
        };
        if(p_falg)  a = ans;
        if(q_falg) b = ans;
        ans++;
    } while( next_permutation(v.begin(), v.end()) );     // 次の順列を生成
    ans = abs(a-b);
    cout <<ans<<endl;
}


Measured memory_bytes: 256