#include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; const int MAXN = 4e5; int N, A[MAXN+10], B[MAXN+10]; int PA[MAXN+10], PB[MAXN+10]; vector comp; struct SEG { pii tree[MAXN*4+10]; void update(int node, int tl, int tr, int l, int r, int k) { if(l>r) return; if(r>1; update(node*2, tl, mid, l, r, k); update(node*2+1, mid+1, tr, l, r, k); if(tree[node].first) tree[node].second=tr-tl+1; else tree[node].second=tree[node*2].second+tree[node*2+1].second; } }seg; int main() { scanf("%d", &N); for(int i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]); for(int i=1; i<=N; i++) scanf("%d", &B[i]), comp.push_back(B[i]); sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for(int i=1; i<=N; i++) A[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1; for(int i=1; i<=N; i++) B[i]=lower_bound(comp.begin(), comp.end(), B[i])-comp.begin()+1; ll ans=0; for(int i=1; i<=N; i++) { int l, r; vector V; V.push_back(A[i]); if(A[i]!=B[i]) V.push_back(B[i]); for(auto it : V) { l=PA[it]; r=PB[it]; if(l>r) swap(l, r); seg.update(1, 1, N, l+1, r, -1); } PA[A[i]]=i; PB[B[i]]=i; for(auto it : V) { l=PA[it]; r=PB[it]; if(l>r) swap(l, r); seg.update(1, 1, N, l+1, r, 1); } ans+=i-seg.tree[1].second; } printf("%lld\n", ans); }