import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
    std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
    std.random, std.typecons, std.container;

ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l    ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);

void main()
{
    long n = lread();
    auto p = aryread();
    auto q = aryread();
    long iter, p_n, q_n;
    auto tmp = iota(n).map!(x => x + 1).array();
    while (tmp.nextPermutation())
    {
        iter++;
        if (p == tmp)
            p_n = iter;
        if (q == tmp)
            q_n = iter;
    }
    writeln(abs(p_n - q_n));
}

bool is_reversable(string s)
{
    bool ret = true;
    foreach (i; iota(s.length / 2))
    {
        ret &= (s[i] == s[$ - 1 - i]);
    }
    return ret;
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}
