#include <bits/stdc++.h>
#define maxn 100010
using namespace std;

int fwt[maxn];
vector<pair<int, int> > b, a;
deque<int> dq;

int sum(int x){
    int res = 0;
    for(; x; x -= (x&-x)){
        res += fwt[x];
    }
    return res;
}

void ubaci(int x){
    for(; x < maxn; x += (x&-x)){
        ++fwt[x];
    }
}

long long Resi(int N, int *A, int *B){
    for(int i = 0; i < N; ++i){
        a.push_back({A[i], i+1});
        b.push_back({B[i], i+1});
    }
    sort(a.begin(), a.end());
    int ans = 0;
    for(int i = 0; i < N; ++i){
        int tmp = a[i].second;
        tmp--;
        ans += min(sum(B[tmp]), i-sum(B[tmp]));
        ubaci(B[tmp]);
    }
    return ans;
};