#include #define rep(i,n) for (int i = 0; i < (int)(n); i ++) #define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i) using namespace std; using ll = long long; using PL = pair; using P = pair; constexpr int INF = 1000000000; constexpr long long HINF = 1000000000000000; constexpr long long MOD = 1000000007;// = 998244353; constexpr double EPS = 1e-4; constexpr double PI = 3.14159265358979; //T is longlong or int //1-indexed template struct BinaryIndexedTree { int N,power = 1; vector bit; BinaryIndexedTree(int N = 0): N(N){ bit.assign(N + 1,0); while (power <= N) power <<= 1; //power > N } //add x to a_i void add(int i,T x) { for (int idx = i; idx <= N; idx += (idx & -idx)) { bit[idx] += x; } } //return sum of a_1 + a_2 + a_3 + .. + a_k T sum(int k) { T ret = 0; for (int idx = k; idx > 0; idx -= (idx & -idx)) { ret += bit[idx]; } return ret; } // return min index s.t. a_1 + a_2 + a_3 + .. + a_x >= w int lower_bound(T w) { if (w <= 0) return 0; int x = 0; for (int r = power; r > 0; r >>= 1) { if (x + r <= N && bit[x + r] < w) { w -= bit[x + r]; x += r; } } return x + 1; } }; int main() { int N; cin >> N; vector A(N),B(N),C(N + 1); rep(i,N) cin >> A[i]; rep(i,N) cin >> B[i]; rep(i,N) C[B[i]] = i; rep(i,N) A[i] = C[A[i]]; BinaryIndexedTree bit(N); ll ans = 0; rep(i,N) { ans += i - bit.sum(A[i] + 1); bit.add(A[i] + 1,1); } cout << ans << '\n'; return 0; }