#include using namespace std; // Optimization stuff inline void debugMode() { #ifndef ONLINE_JUDGE freopen("debug.in", "r", stdin); #endif // ONLINE_JUDGE } inline void optimizeIt() { ios::sync_with_stdio(false); cin.tie(NULL); } // End optimization stuff inline double ABS(const int &x) { return max(x, -x); } inline bool isPrime(const long long int &x) { if(x == 1) return false; for(long long int d = 2; d * d <= x; d++) { if(x % d == 0) return false; } return true; } inline int GCD(int a, int b) { while(b) { int r = a % b; a = b; b = r; } return a; } typedef long long int ll; typedef long double ld; const int NMax = 50000 + 5; const int LIM = 1e5; const int MOD = 1e9 + 7; int norma[NMax]; int A[NMax], B[NMax]; deque < int > dq; inline int Binary(const int &val) { int lo = 0; int hi = (int)dq.size() - 1; int best = (int)dq.size() - 1; int n = (int)dq.size(); while(lo <= hi) { int mid = (lo + hi) >> 1; if(dq[mid] <= val) { lo = mid + 1; } else { hi = mid - 1; best = mid; } } return n - (best + 1); } int main(){ debugMode(); optimizeIt(); int n; cin >> n; int k = 0; for(int i = 1; i <= n; i++) { cin >> A[i]; if(norma[A[i]] == 0) { norma[A[i]] = ++k; A[i] = k; } else { A[i] = norma[A[i]]; } } for(int i = 1; i <= n; i++) { cin >> B[i]; if(norma[B[i]] == 0) { norma[B[i]] = ++k; B[i] = k; } else { B[i] = norma[B[i]]; } } int ans = 0; for(int i = 1; i <= n; i++) { sort(dq.begin(), dq.end()); ans += Binary(B[i]); dq.push_back(B[i]); } cout << ans; return 0; }