#include<bits/stdc++.h> #define re register #define int long long
usingnamespace std;
constint N = 1e5 + 10; int n,ans,t; int arr[N],brr[N],vis[N],pos[N];
inlineintread(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
structBIT{ int tr[N];
inlineintlowbit(int x){ return x & -x; }
inlinevoidmodify(int x,int k){ for (re int i = x;i <= n;i += lowbit(i)) tr[i] += k; }
inlineintquery(int x){ int res = 0; for (re int i = x;i;i -= lowbit(i)) res += tr[i]; return res; } }tree;
inlineintf(int *arr,int *brr){ for (re int i = 1;i <= n;i++) vis[arr[i]] = i; for (re int i = 1;i <= n;i++) pos[i] = vis[brr[i]]; int res = 0,ans = 0; memset(tree.tr,0,sizeof(tree.tr)); for (re int i = n;i;i--){ res += tree.query(pos[i]); tree.modify(pos[i],1); } ans = res; for (re int i = n;i;i--){ res = res - n + 2 * pos[i] - 1; ans = min(ans,res); } return ans; }
signedmain(){ n = read(); for (re int i = 1;i <= n;i++) arr[i] = read(); for (re int i = 1;i <= n;i++) brr[i] = read(); printf("%lld",min(f(arr,brr),f(brr,arr))); return0; }