#define ANSWER
#include <algorithm>
#include <vector>
#include <iostream>
#include <numeric>
#include <cmath>


template <typename T> 
class mylist {
    private:
        std::vector<char> alphabets = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 
    public:
        mylist(long long i)
        {
            list.resize(i);
        }
        mylist()
        {}
        std::vector<T> list;
        void sort(){
            std::sort(list.begin(), list.end());
        };
        std::vector<T> sorted();
        // std::vector<size_t> argsort(){
        //     std::vector<size_t> index(list.size());
        //     std::iota(index.begin(), index.end(), 0);

        //     std::sort(index.begin(), index.end(), [&list](size_t i1, size_t i2) {
        //         return list[i1] < list[i2];
        //     });

        //     return index;
        // };
        int find(T key){
            typename std::vector<T>::iterator iter = std::find(list.begin(), list.end(), key);
            return std::distance(list.begin(), iter);
        };
        std::string toString(){
            std::string string(list.begin(), list.end());
            return string;
        }
        void fromString(std::string string){
            mylist<char> S;
            std::copy(string.begin(), string.end(), std::back_inserter(list));
        }
        void fromAlphabets(std::string string){
            std::transform(string.begin(), string.end(), string.begin(), ::tolower);
            for (size_t i = 0; i < string.size(); i++)
            {
                typename std::vector<char>::iterator iter = std::find(alphabets.begin(), alphabets.end(), string[i]);
                list.push_back(std::distance(alphabets.begin(), iter));
            }            
        }
        std::string toAlphabets(bool upper){
            std::string alp;
            for (auto &&i : list)
            {
                alp.push_back(alphabets[i - std::floor(i / alphabets.size()) * alphabets.size()]);
            }
            if (upper)
            {
                std::transform(alp.begin(), alp.end(), alp.begin(), ::toupper);
            }
            
            return alp;     
        }

        template <typename V> 
        void add(V value){
            for (auto &&i : list)
            {
                i+=value;
            }
        }
};

#include <iostream>
#include <vector>
#include <cstdarg>
#include <cmath>
#include <string>

class mystd
{
public:
    template<typename T>
    static bool Input(int number,...);
    template<typename T>
    static bool InputArray(std::vector<T> &array);
    static bool InputArrays(long long number,...);
    static bool InputMatrix(std::vector<std::vector<long long>> array);
    static long long min(std::vector<long long> array);
    static long long max(std::vector<long long> array);
    template<typename T>
    static void Print(int number,...);
    template<typename T>
    static void PrintArray(std::vector<T> array,const std::string sep);
};

template<typename T>
bool mystd::Input(int number,...){
    va_list args;
    va_start(args, number);
    for (long long i = 0; i < number; i++)
    {
        std::cin >> *(va_arg(args, T*));
    }
    return true;
}

template<typename T>
void mystd::Print(int number,...){
    va_list args;
    va_start(args, number);
    for (long long i = 0; i < number; i++)
    {
        if(i!=0){
            std::cout << " ";
        }
        std::cout << *(va_arg(args, T*));
    }
    std::cout << std::endl;;
}

template<typename T>
void mystd::PrintArray(std::vector<T> array,const std::string sep){
    for (long long i = 0; i < array.size(); i++)
    {
        if(i!=0){
            std::cout << sep;
        }
        std::cout << array[i];
    }
    std::cout << std::endl;;
}

template<typename T>
bool mystd::InputArray(std::vector<T> &array){
    for (size_t i = 0; i < array.size(); i++)
    {
        std::cin >> array[i];
    }
    return true;
}

bool mystd::InputArrays(long long number,...){
    va_list args;
    va_start(args, number);
    std::vector<std::vector<long long>*> arrays(number);
    for (auto &i : arrays)
    {
        i = va_arg(args, std::vector<long long>*);
    }
    long long length = arrays[0]->size();

    for (long long j=0;j<length;j++)
    {
        for (auto &&i : arrays)
        {
            std::cin >> (*i)[j];
        }
    }
    return true;
}


long long mystd::min(std::vector<long long> array){
    long long ans = array[0];
    for (long long &i : array)
    {
        if(i < ans){
            ans = i;
        }
    }
    return ans;
}


long long mystd::max(std::vector<long long> array){
    long long ans = array[0];
    for (long long &i : array)
    {
        if(i > ans){
            ans = i;
        }
    }
    return ans;
}

// #include <algorithm>
// #include <vector>
// #include <iostream>
// #include <numeric>
// #include <string>
 
// class mystring {
//     public:
//         std::string str;
//         void sort(); 
//         std::string sorted();
//         std::string argsort();
//         int find(std::string key){
//             typename std::string::iterator iter = std::find(string.begin(), string.end(), key);
//             return std::distance(string.begin(), iter);
//         };
// };

#ifndef ANSWER
#include "../../library/mystd.hpp"
#include "../../library/mylist.hpp"
#endif

typedef long long ll;
typedef std::vector<long long> vll;
typedef long double ld;
typedef std::vector<long double> vld;

void Main(){
    ll N;
    mystd::Input<ll>(1,&N);
    mylist<ll> P(N),Q(N),iP(N),iQ(N);
    mystd::InputArray<ll>(P.list);
    mystd::InputArray<ll>(Q.list);

    ll memo = 1;
    for (size_t i = 1; i <= N; i++)
    {
        memo *= i;
    }

    ll ansP = 0;
    ll ansQ = 0;
    std::iota(iP.list.begin(), iP.list.end(), 1);
    std::iota(iQ.list.begin(), iQ.list.end(), 1);
    for (size_t i = N-1; i >= 1; i--)
    {
        memo /= i+1;
        ll j = iP.find(P.list[N-1-i]);
        ansP += j * memo;

        iP.list.erase(iP.list.begin()+j);
        j = iQ.find(Q.list[N-1-i]);

        ansQ += j * memo;
        
        iQ.list.erase(iQ.list.begin()+j);
    }
    ll ans =std::abs(ansP-ansQ);

    mystd::Print<ll>(1,&ans);
}

int main(int argc, char const *argv[])
{
    Main();
    return 0;
}

Measured memory_bytes: 256