module app;

import core.bitop;

import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.math;
import std.stdio;
import std.string;

struct Input
{
	int n;
	int[] p, q;
}

void parseInput(T)(out Input input, T file)
{
	with (file) with (input)
	{
		n = readln().strip().to!int;
		p = readln().strip().split().map!(to!int).array;
		q = readln().strip().split().map!(to!int).array;
	}
}

struct Output
{
}

auto main2(Input* input)
{
	return abs(orderOf(input.p) - orderOf(input.q));
}

int orderOf(int[] p)
{
    bool[] temp;
    temp.length = p.length;

    int order = 0;

    foreach (i, e; p)
    {
        auto value = factorial(p.length - i - 1);
        for (int j = 0; j < e; j++)
        {
            if (temp[j] == false)
                order += value;
        }
        temp[e-1] = true;
    }

    return order;
}

ulong factorial(ulong n)
{
    uint ret = 1;
    foreach (i; 1..n+1)
        ret *= i;
    return ret;
}

unittest { writeln("begin unittest"); }

unittest // example1
{
	string example =
`3
1 3 2
3 1 2`;
	Input input = void;
	parseExample(input, example);
	auto result = main2(&input);
	writeln(result);
	assert(result == 3);
}

unittest // example2
{
	string example =
`8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1`;
	Input input = void;
	parseExample(input, example);
	auto result = main2(&input);
	writeln(result);
	assert(result == 17517);
}

unittest // example3
{
	string example =
`3
1 2 3
1 2 3`;
	Input input = void;
	parseExample(input, example);
	auto result = main2(&input);
	writeln(result);
	assert(result == 0);
}

unittest { writeln("end unittest"); }

void parseExample(out Input input, string example)
{
	struct Adapter
	{
		string[] _lines;
		this(string input) { _lines = input.splitLines(); }
		string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
	}
	parseInput(input, Adapter(example));
}

void main()
{
	Input input = void;
	parseInput(input, stdin);
	auto result = main2(&input);
	writeln(result);
}
