#include <bits/stdc++.h>
using namespace std;
const int INF = 1e+9 + 17;
struct Segment_Tree
{
vector <int> a, b;
vector <pair <int, int> > T;
int sz;
pair <int, int> ans;
Segment_Tree(vector <int> &x, vector <int> &y)
{
sz = x.size();
a = x;
b = y;
T.resize((sz << 2), {INF, -INF});
build(1, 0, sz - 1);
}
void build (int v, int l, int r)
{
if (l > r)
return;
if (l == r)
{
T[v] = {b[l], a[r]};
return;
}
int m = (l + r) >> 1;
build(2*v, l, m);
build(2*v + 1, m + 1, r);
T[v].first = min(T[2*v].first, T[2*v + 1].first);
T[v].second = max(T[2*v].second, T[2*v + 1].second);
}
void get_ans(int v, int L, int R, int l, int r)
{
if (L > R)
return;
if (l > r)
return;
if (L == l && r == R)
{
ans.first = min(ans.first, T[v].first), ans.second = max(ans.second, T[v].second);
return;
}
int m = (L + R) >> 1;
get_ans(2*v, L, m, l, min(r, m));
get_ans(2*v + 1, m + 1, R, max(l, m + 1), r);
}
int get_ans(int l, int r) {
ans = {INF, -INF};
get_ans(1, 0, sz - 1, l, r);
return ans.second - ans.first;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long ANS = 0;
cin >> n;
vector <int> a(n);
vector <int> b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
Segment_Tree t = Segment_Tree(a, b);
for (int l = 0; l < n; l++)
{
int L = l - 1, R = n - 1;
while (R - L > 1)
{
int m = (L + R) >> 1;
if (t.get_ans(l, m) >= 0)
R = m;
else
L = m;
}
if (t.get_ans(l, R) == 0)
{
int l1 = R - 1, r1 = n;
while (r1 - l1 > 1)
{
int m = (l1 + r1) >> 1;
if (t.get_ans(l, m))
r1 = m;
else
l1 = m;
}
ANS += r1 - R;
}
}
cout << ANS << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IElORiA9IDFlKzkgKyAxNzsKCnN0cnVjdCBTZWdtZW50X1RyZWUKewogICAgdmVjdG9yIDxpbnQ+IGEsIGI7CiAgICB2ZWN0b3IgPHBhaXIgPGludCwgaW50PiA+IFQ7CgogICAgaW50IHN6OwogICAgcGFpciA8aW50LCBpbnQ+IGFuczsKCiAgICBTZWdtZW50X1RyZWUodmVjdG9yIDxpbnQ+ICZ4LCB2ZWN0b3IgPGludD4gJnkpCiAgICB7CiAgICAgICAgc3ogPSB4LnNpemUoKTsKICAgICAgICBhID0geDsKICAgICAgICBiID0geTsKICAgICAgICBULnJlc2l6ZSgoc3ogPDwgMiksIHtJTkYsIC1JTkZ9KTsKICAgICAgICBidWlsZCgxLCAwLCBzeiAtIDEpOwogICAgfQoKICAgIHZvaWQgYnVpbGQgKGludCB2LCBpbnQgbCwgaW50IHIpCiAgICB7CiAgICAgICAgaWYgKGwgPiByKQogICAgICAgICAgICByZXR1cm47CiAgICAgICAgaWYgKGwgPT0gcikKICAgICAgICB7CiAgICAgICAgICAgIFRbdl0gPSB7YltsXSwgYVtyXX07CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIGludCBtID0gKGwgKyByKSA+PiAxOwoKICAgICAgICBidWlsZCgyKnYsIGwsIG0pOwogICAgICAgIGJ1aWxkKDIqdiArIDEsIG0gKyAxLCByKTsKCiAgICAgICAgVFt2XS5maXJzdCA9IG1pbihUWzIqdl0uZmlyc3QsIFRbMip2ICsgMV0uZmlyc3QpOwogICAgICAgIFRbdl0uc2Vjb25kID0gbWF4KFRbMip2XS5zZWNvbmQsIFRbMip2ICsgMV0uc2Vjb25kKTsKICAgIH0KCiAgICB2b2lkIGdldF9hbnMoaW50IHYsIGludCBMLCBpbnQgUiwgaW50IGwsIGludCByKQogICAgewogICAgICAgIGlmIChMID4gUikKICAgICAgICAgICAgcmV0dXJuOwoKICAgICAgICBpZiAobCA+IHIpCiAgICAgICAgICAgIHJldHVybjsKCiAgICAgICAgaWYgKEwgPT0gbCAmJiByID09IFIpCiAgICAgICAgewogICAgICAgICAgICBhbnMuZmlyc3QgPSBtaW4oYW5zLmZpcnN0LCAgVFt2XS5maXJzdCksIGFucy5zZWNvbmQgPSBtYXgoYW5zLnNlY29uZCwgVFt2XS5zZWNvbmQpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGludCBtID0gKEwgKyBSKSA+PiAxOwoKICAgICAgICBnZXRfYW5zKDIqdiwgTCwgbSwgbCwgbWluKHIsIG0pKTsKICAgICAgICBnZXRfYW5zKDIqdiArIDEsIG0gKyAxLCBSLCBtYXgobCwgbSArIDEpLCByKTsKICAgIH0KCiAgICBpbnQgZ2V0X2FucyhpbnQgbCwgaW50IHIpIHsKICAgICAgICBhbnMgPSB7SU5GLCAtSU5GfTsKCiAgICAgICAgZ2V0X2FucygxLCAwLCBzeiAtIDEsIGwsIHIpOwoKICAgICAgICByZXR1cm4gYW5zLnNlY29uZCAtIGFucy5maXJzdDsKICAgIH0KfTsKCmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwoKICAgIGludCBuOwogICAgbG9uZyBsb25nIEFOUyA9IDA7CiAgICBjaW4gPj4gbjsKCiAgICB2ZWN0b3IgPGludD4gYShuKTsKICAgIHZlY3RvciA8aW50PiBiKG4pOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSBjaW4gPj4gYVtpXTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSBjaW4gPj4gYltpXTsKCiAgICBTZWdtZW50X1RyZWUgdCA9IFNlZ21lbnRfVHJlZShhLCBiKTsKCiAgICBmb3IgKGludCBsID0gMDsgbCA8IG47IGwrKykKICAgIHsKICAgICAgICBpbnQgTCA9IGwgLSAxLCBSID0gbiAtIDE7CgogICAgICAgIHdoaWxlIChSIC0gTCA+IDEpCiAgICAgICAgewogICAgICAgICAgICBpbnQgbSA9IChMICsgUikgPj4gMTsKICAgICAgICAgICAgaWYgKHQuZ2V0X2FucyhsLCBtKSA+PSAwKQogICAgICAgICAgICAgICAgUiA9IG07CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIEwgPSBtOwogICAgICAgIH0KCiAgICAgICAgaWYgKHQuZ2V0X2FucyhsLCBSKSA9PSAwKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGwxID0gUiAtIDEsIHIxID0gbjsKICAgICAgICAgICAgd2hpbGUgKHIxIC0gbDEgPiAxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgbSA9IChsMSArIHIxKSA+PiAxOwogICAgICAgICAgICAgICAgaWYgKHQuZ2V0X2FucyhsLCBtKSkKICAgICAgICAgICAgICAgICAgICByMSA9IG07CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgbDEgPSBtOwogICAgICAgICAgICB9CiAgICAgICAgICAgIEFOUyArPSByMSAtIFI7CiAgICAgICAgfQogICAgfQoKICAgIGNvdXQgPDwgQU5TIDw8IGVuZGw7Cn0K