unittest
{
	assert( [ "3", "1 3 2", "3 1 2" ].parse.expand.solve == 3 );
	assert( [ "8", "7 3 5 4 2 1 6 8", "3 8 2 5 4 6 7 1" ].parse.expand.solve == 17517 );
	assert( [ "3", "1 2 3", "1 2 3" ].parse.expand.solve == 0 );
}



import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	input.popFront;
	auto ps = input.front.split.to!( long[] );
	input.popFront;
	auto qs = input.front.split.to!( long[] );
	return tuple( ps, qs );
}

auto solve( long[] ps, long[] qs )
{
	auto pi = -1L;
	auto qi = -1L;
	auto as = iota( 1L, ps.length.signed + 1 ).array;
	for( auto i = 0L; as.nextPermutation; i++ )
	{
		if( as == ps ) pi = i;
		if( as == qs ) qi = i;
	}
	return ( qi - pi ).abs;
}
