#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max()/32*15+208; template class BIT { vector bit; int m; public: BIT(int n): bit(vector(n+1, 0)){ m = 1; while(m < n) m <<= 1; } T sum(int k){ T ret = 0; for (++k; k > 0; k -= (k & -k)) ret += bit[k]; return ret; } void add(int k, T x){ for (++k; k < bit.size(); k += (k & -k)) bit[k] += x; } T lower_bound(T x){ int i = 0; for (int j = m; j > 0; j >>= 1) { if(i+j < bit.size() && bit[i+j] < x) x -= bit[i += j]; } return i; } }; int main() { int n; cin >> n; vector A(n), B(n); for (auto &&i : A) scanf("%d", &i), i--; for (auto &&j : B) scanf("%d", &j), j--; vector v(n); for (int i = 0; i < n; ++i) { v[A[i]] = i; } for (int i = 0; i < n; ++i) { B[i] = v[B[i]]; } ll ans = 0; BIT S(n); for (int i = 0; i < n; ++i) { ans += i-S.sum(B[i]); S.add(B[i], 1); } cout << ans << "\n"; return 0; }