import std.algorithm, std.array, std.conv, std.stdio, std.string;

void main()
{
    readln;
    auto p = readln.chomp.split.to!(int[]);
    auto q = readln.chomp.split.to!(int[]);
    if (p > q)
    {
        auto r = p;
        p = q;
        q = r;
    }
    int ans;
    while (p != q)
    {
        p.nextPermutation;
        ans += 1;
    }
    ans.writeln;
}
