#include #include #include using namespace std; int n, a[200006], b[200006], lt[400006], rt[400006]; pair tree[524288]; void update(int i, int b, int e, int l, int r, int p) { if (r < b || e < l) return; int m = (b + e) / 2; if (l <= b && e <= r) tree[i].second += p; else update(i * 2 + 1, b, m, l, r, p), update(i * 2 + 2, m + 1, e, l, r, p); if (tree[i].second) tree[i].first = e - b + 1; else if (b == e) tree[i].first = 0; else tree[i].first = tree[i * 2 + 1].first + tree[i * 2 + 2].first; } void update_rev(int x) { int L = min(lt[x], rt[x]) + 1; int R = max(lt[x], rt[x]); if (L <= R) update(0, 0, 262143, L, R, -1); } void update_for(int x) { int L = min(lt[x], rt[x]) + 1; int R = max(lt[x], rt[x]); if (L <= R) update(0, 0, 262143, L, R, 1); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i), a[i]--; for (int i = 0; i < n; i++) scanf("%d", b + i), b[i]--; for (int i = 0; i < 400006; i++) lt[i] = rt[i] = -1; long long res = 0; for (int i = 0; i < n; i++) { update_rev(a[i]); lt[a[i]] = max(lt[a[i]], i); update_for(a[i]); update_rev(b[i]); rt[b[i]] = max(rt[b[i]], i); update_for(b[i]); res += i + 1 - tree[0].first; } printf("%lld", res); }