#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vi a(N), b(N);
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] != b[i]) continue;
        int low = i+1, high = N+1;
        while (high - low > 1) {
            const int med = (low+high)/2;
            if (A.query(i, med) <= B.query(i, med)) low = med;
            else high = med;
        }
        if (A.query(i, low) == B.query(i, low)) ans += low-i;
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vi a(N), b(N);
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] != b[i]) continue;
        int ub = -1, lb = -1;
        // ub
        {
            int low = i+1, high = N+1;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(i, med) == B.query(i, med)) low = med;
                else high = med;
            }
            ub = low;
        }
        // lb
        {
            int low = -1, high = i;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(med, i) < a[i] && B.query(med, i) > b[i]) high = med;
                else low = med;
            }
            lb = high;
        }
        ans += (ll)(ub-i)*(i-lb+1);
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};
int a[200200], b[200200];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] > b[i]) continue;
        // max(a[l..r)) == min(b[l..r)) を満たす最大の r を求める
        int maxR = -1;
        {
            int low = i+1, high = N;
            while (high - low > 1) {
                const int med = (low + high) / 2;
                if (A.query(i, med) <= B.query(i, med)) low = med;
                else high = med;
            }
            maxR = low;
        }
        if (A.query(i, maxR) != B.query(i, maxR)) continue;
        int minR = -1;
        {
            int low = i, high = maxR;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(i, med) == B.query(i, med)) high = med;
                else low = med;
            }
            minR = high;
        }
        ans += maxR - minR + 1;
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vi a(N), b(N);
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] != b[i]) continue;
        int ub = -1, lb = -1;
        {
            int low = i+1, high = N+1;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(i, med) <= B.query(i, med)) low = med;
                else high = med;
            }
            ub = low;
        }
        {
            int low = -1, high = i;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(med, i) <= B.query(med, i)) high = med;
                else low = med;
            }
            lb = high;
        }
        ans += ub-lb;
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<utility>
#include<cassert>

// const int dx[] = {1, 0, -1, 0};
// const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
// typedef unsigned long long ull;
// typedef vector<int> vi;
// typedef vector<ll> vll;
// typedef pair<int, int> pii;

// // セグメント木(RMQ 対応)
// // update: k 番目の値を a に変更
// // query: [l, r) の区間の最大値を求める
// template<typename T>
// struct ST {
//     vector<T> seg;
//     int size;
//     ST(int n) {
//         size = 1;
//         while (size < n) size *= 2;
//         seg.resize(2*size-1, numeric_limits<T>::min());
//     }
//     inline T merge(T x, T y) {
//         return max(x, y);
//     }
//     void update(int k, T a) {
//         k += size-1;
//         seg[k] = a;
//         while (k > 0) {
//             k = (k-1)/2;
//             seg[k] = merge(seg[k*2+1], seg[k*2+2]);
//         }
//     }
//     T query(int a, int b, int k, int l, int r) {
//         if (r <= a || b <= l) return numeric_limits<T>::min();
//         if (a <= l && r <= b) return seg[k];
//         T vl = query(a, b, k*2+1, l, (l+r)/2);
//         T vr = query(a, b, k*2+2, (l+r)/2, r);
//         return merge(vl, vr);
//     }
//     T query(int a, int b) {
//         return query(a, b, 0, 0, size);
//     }
// };

// // セグメント木(RMQ 対応)
// // update: k 番目の値を a に変更
// // query: [l, r) の区間の最大値を求める
// template<typename T>
// struct ST2 {
//     vector<T> seg;
//     int size;
//     ST2(int n) {
//         size = 1;
//         while (size < n) size *= 2;
//         seg.resize(2*size-1, numeric_limits<T>::max());
//     }
//     inline T merge(T x, T y) {
//         return min(x, y);
//     }
//     void update(int k, T a) {
//         k += size-1;
//         seg[k] = a;
//         while (k > 0) {
//             k = (k-1)/2;
//             seg[k] = merge(seg[k*2+1], seg[k*2+2]);
//         }
//     }
//     T query(int a, int b, int k, int l, int r) {
//         if (r <= a || b <= l) return numeric_limits<T>::max();
//         if (a <= l && r <= b) return seg[k];
//         T vl = query(a, b, k*2+1, l, (l+r)/2);
//         T vr = query(a, b, k*2+2, (l+r)/2, r);
//         return merge(vl, vr);
//     }
//     T query(int a, int b) {
//         return query(a, b, 0, 0, size);
//     }
// };
// int a[200200], b[200200];

// int main() {
//     int N;
//     cin >> N;
//     ST<int> A(N);
//     ST2<int> B(N);
//     for (int i = 0; i < N; i++) {
//         scanf("%d", a+i);
//         A.update(i, a[i]);
//     }
//     for (int i = 0; i < N; i++) {
//         scanf("%d", b+i);
//         B.update(i, b[i]);
//     }
//     ll ans = 0;
//     for (int i = 0; i < N; i++) {
//         if (a[i] > b[i]) continue;
//         // max(a[l..r)) == min(b[l..r)) を満たす最大の r を求める
//         int maxR = -1;
//         {
//             int low = i+1, high = N;
//             while (high - low > 1) {
//                 const int med = (low + high) / 2;
//                 if (A.query(i, med) <= B.query(i, med)) low = med;
//                 else high = med;
//             }
//             maxR = low;
//         }
//         if (A.query(i, maxR) != B.query(i, maxR)) continue;
//         int minR = -1;
//         {
//             int low = i, high = maxR;
//             while (high - low > 1) {
//                 const int med = (low+high)/2;
//                 if (A.query(i, med) == B.query(i, med)) high = med;
//                 else low = med;
//             }
//             minR = high;
//         }
//         ans += maxR - minR + 1;
//     }
//     printf("%lld\n", ans);
//     return 0;
// }

# define scan(x) scanf("%d",&x)
# define fi cin
# define fo cout
int a[1 << 20];
int b[1 << 20];
int Sa[1 << 20];
int Sb[1 << 20];
int n;
int lg[1 << 20];
void updA(int i,int val)
{
    for (;i <= n;i += i&(-i)) Sa[i] = max(Sa[i],val);
}
int quA(int i)
{
    int ans = -(1e9+1);
    for (;i;i -= i&(-i)) ans = max(ans,Sa[i]);
    return ans;
}
void updB(int i,int val)
{
    for (;i <= n;i += i&(-i)) Sb[i] = min(Sb[i],val);
}
int quB(int i)
{
    int ans = (1e9+1);
    for (;i;i -= i&(-i)) ans = min(ans,Sb[i]);
    return ans;
}
int mx(int T)
{
    return quA(T);
}
int mn(int T)
{
    return quB(T);
}
int main(void)
{
    for (int i = 2;i <= 1e6;++i) lg[i] = lg[i/2] + 1;
    scan(n);
    for (int i = 1;i <= n;++i) scan(a[i]),Sa[i] =-(1e9+1);
    for (int i = 1;i <= n;++i) scan(b[i]),Sb[i] = (1e9+1);
    ll ans = 0;
    for (int i = n;i;--i)
    {
        updA(i,a[i]);
        updB(i,b[i]);
        if (mx(i) > mn(i)) continue;
        int l = i-1,r = i;
        for (int p = 1 << lg[n - i + 1];p;p >>= 1)
            {
                if (p + l <= n && mx(l+p) < mn(l+p)) l += p;
                if (p + r <= n && mx(r+p) <= mn(r+p)) r += p;
            }
        ans += r - l;
    }
    fo << ans << '\n';
    return 0;
}


// #include<cstdio>
// #include<cstdlib>
// #include<cstring>
// #include<cmath>
// //#include<cctype>
// #include<climits>
// #include<iostream>
// #include<string>
// #include<vector>
// #include<map>
// //#include<list>
// #include<queue>
// #include<deque>
// #include<algorithm>
// //#include<numeric>
// #include<utility>
// //#include<memory>
// #include<functional>
// #include<cassert>
// #include<set>
// #include<stack>
// #include<random>

// const int dx[] = {1, 0, -1, 0};
// const int dy[] = {0, -1, 0, 1};
// using namespace std;
// typedef long long ll;
// typedef unsigned long long ull;
// typedef vector<int> vi;
// typedef vector<ll> vll;
// typedef pair<int, int> pii;

// // セグメント木(RMQ 対応)
// // update: k 番目の値を a に変更
// // query: [l, r) の区間の最大値を求める
// template<typename T>
// struct ST {
//     vector<T> seg;
//     int size;
//     ST(int n) {
//         size = 1;
//         while (size < n) size *= 2;
//         seg.resize(2*size-1, numeric_limits<T>::min());
//     }
//     inline T merge(T x, T y) {
//         return max(x, y);
//     }
//     void update(int k, T a) {
//         k += size-1;
//         seg[k] = a;
//         while (k > 0) {
//             k = (k-1)/2;
//             seg[k] = merge(seg[k*2+1], seg[k*2+2]);
//         }
//     }
//     T query(int a, int b, int k, int l, int r) {
//         if (r <= a || b <= l) return numeric_limits<T>::min();
//         if (a <= l && r <= b) return seg[k];
//         T vl = query(a, b, k*2+1, l, (l+r)/2);
//         T vr = query(a, b, k*2+2, (l+r)/2, r);
//         return merge(vl, vr);
//     }
//     T query(int a, int b) {
//         return query(a, b, 0, 0, size);
//     }
// };

// // セグメント木(RMQ 対応)
// // update: k 番目の値を a に変更
// // query: [l, r) の区間の最大値を求める
// template<typename T>
// struct ST2 {
//     vector<T> seg;
//     int size;
//     ST2(int n) {
//         size = 1;
//         while (size < n) size *= 2;
//         seg.resize(2*size-1, numeric_limits<T>::max());
//     }
//     inline T merge(T x, T y) {
//         return min(x, y);
//     }
//     void update(int k, T a) {
//         k += size-1;
//         seg[k] = a;
//         while (k > 0) {
//             k = (k-1)/2;
//             seg[k] = merge(seg[k*2+1], seg[k*2+2]);
//         }
//     }
//     T query(int a, int b, int k, int l, int r) {
//         if (r <= a || b <= l) return numeric_limits<T>::max();
//         if (a <= l && r <= b) return seg[k];
//         T vl = query(a, b, k*2+1, l, (l+r)/2);
//         T vr = query(a, b, k*2+2, (l+r)/2, r);
//         return merge(vl, vr);
//     }
//     T query(int a, int b) {
//         return query(a, b, 0, 0, size);
//     }
// };
// int a[200200], b[200200];

// int main() {
//     int N;
//     cin >> N;
//     ST<int> A(N);
//     ST2<int> B(N);
//     for (int i = 0; i < N; i++) {
//         scanf("%d", a+i);
//         A.update(i, a[i]);
//     }
//     for (int i = 0; i < N; i++) {
//         scanf("%d", b+i);
//         B.update(i, b[i]);
//     }
//     ll ans = 0;
//     for (int i = 0; i < N; i++) {
//         if (a[i] > b[i]) continue;
//         // max(a[l..r)) == min(b[l..r)) を満たす最大の r を求める
//         int maxR = -1;
//         {
//             int low = i+1, high = N;
//             while (high - low > 1) {
//                 const int med = (low + high) / 2;
//                 if (A.query(i, med) <= B.query(i, med)) low = med;
//                 else high = med;
//             }
//             maxR = low;
//         }
//         if (A.query(i, maxR) != B.query(i, maxR)) continue;
//         int minR = -1;
//         {
//             int low = i, high = maxR;
//             while (high - low > 1) {
//                 const int med = (low+high)/2;
//                 if (A.query(i, med) == B.query(i, med)) high = med;
//                 else low = med;
//             }
//             minR = high;
//         }
//         ans += maxR - minR + 1;
//     }
//     printf("%lld\n", ans);
//     return 0;
// }

# include <bits/stdc++.h>
# define scan(x) scanf("%d",&x)
using namespace std;
# define fi cin
# define fo cout
int a[1 << 20];
int b[1 << 20];
int Sa[1 << 20];
int Sb[1 << 20];
int n;
int lg[1 << 20];
void updA(int i,int val)
{
    for (;i <= n;i += i&(-i)) Sa[i] = max(Sa[i],val);
}
int quA(int i)
{
    int ans = -(1e9+1);
    for (;i;i -= i&(-i)) ans = max(ans,Sa[i]);
    return ans;
}
void updB(int i,int val)
{
    for (;i <= n;i += i&(-i)) Sb[i] = min(Sb[i],val);
}
int quB(int i)
{
    int ans = (1e9+1);
    for (;i;i -= i&(-i)) ans = min(ans,Sb[i]);
    return ans;
}
int mx(int T)
{
    return quA(T);
}
int mn(int T)
{
    return quB(T);
}
int main(void)
{
    for (int i = 2;i <= 1e6;++i) lg[i] = lg[i/2] + 1;
    scan(n);
    for (int i = 1;i <= n;++i) scan(a[i]),Sa[i] =-(1e9+1);
    for (int i = 1;i <= n;++i) scan(b[i]),Sb[i] = (1e9+1);
    ll ans = 0;
    for (int i = n;i;--i)
    {
        updA(i,a[i]);
        updB(i,b[i]);
        if (mx(i) > mn(i)) continue;
        int l = i-1,r = i;
        for (int p = 1 << lg[n - i + 1];p;p >>= 1)
            {
                if (p + l <= n && mx(l+p) < mn(l+p)) l += p;
                if (p + r <= n && mx(r+p) <= mn(r+p)) r += p;
            }
        ans += r - l;
    }
    fo << ans << '\n';
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};
int a[200200], b[200200];

int main() {
    int N;
    cin >> N;
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        scanf("%d", a+i);
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        scanf("%d", b+i);
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] > b[i]) continue;
        // max(a[l..r)) == min(b[l..r)) を満たす最大の r を求める
        int maxR = -1;
        {
            int low = i+1, high = N;
            while (high - low > 1) {
                const int med = (low + high) / 2;
                if (A.query(i, med) <= B.query(i, med)) low = med;
                else high = med;
            }
            maxR = low;
        }
        if (A.query(i, maxR) != B.query(i, maxR)) continue;
        int minR = -1;
        {
            int low = i, high = maxR;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(i, med) == B.query(i, med)) high = med;
                else low = med;
            }
            minR = high;
        }
        ans += maxR - minR + 1;
    }
    printf("%lld\n", ans);
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vi a(N), b(N);
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] > b[i]) continue;
        int low = i+1, high = N+1;
        while (high - low > 1) {
            const int med = (low+high)/2;
            if (A.query(i, med) <= B.query(i, med)) low = med;
            else high = med;
        }
        if (A.query(i, low) == B.query(i, low)) ans += low-i;
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST {
    vector<T> seg;
    int size;
    ST(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::min());
    }
    inline T merge(T x, T y) {
        return max(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::min();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

// セグメント木(RMQ 対応)
// update: k 番目の値を a に変更
// query: [l, r) の区間の最大値を求める
template<typename T>
struct ST2 {
    vector<T> seg;
    int size;
    ST2(int n) {
        size = 1;
        while (size < n) size *= 2;
        seg.resize(2*size-1, numeric_limits<T>::max());
    }
    inline T merge(T x, T y) {
        return min(x, y);
    }
    void update(int k, T a) {
        k += size-1;
        seg[k] = a;
        while (k > 0) {
            k = (k-1)/2;
            seg[k] = merge(seg[k*2+1], seg[k*2+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return numeric_limits<T>::max();
        if (a <= l && r <= b) return seg[k];
        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return merge(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vi a(N), b(N);
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        A.update(i, a[i]);
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
        B.update(i, b[i]);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (a[i] != b[i]) continue;
        int ub = -1, lb = -1;
        {
            int low = i+1, high = N+1;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(i, med) <= B.query(i, med)) low = med;
                else high = med;
            }
            ub = low;
        }
        {
            int low = -1, high = i;
            while (high - low > 1) {
                const int med = (low+high)/2;
                if (A.query(med, i+1) <= B.query(med, i+1)) high = med;
                else low = med;
            }
            lb = high;
        }
        ans += ub-lb;
    }
    cout << ans << endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}

int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			if (Max(i, i) <= Min(i, i)){
				int l = i - 1, r = i;
				for (int j = 1 << lg2[n - i]; j ; j >>= 1){
					if (l + j < n && Max(i, l + j) < Min(i, l + j)) l += j;
					if (r + j < n && Max(i, r + j) <= Min(i, r + j)) r += j;
				}
				ans += r - l;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(s, mid) > Max(s, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(s, mid) >= Max(s, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int lo = st, hi = end - 1 , res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if ( mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

int Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			if (low != -1)
				ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();
		/*

ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

*/
		ll ans = 0;
		for (int i = 0; i < n; i++){
			int lo = i, hi = n - 1, resr = 0,resl = -1;
			while (lo <= hi){
				int mid = (lo + hi) / 2;
				int mx = Max(lo, mid), mn = Min(lo, mid);
				if (mx == mn) resr = mid, hi = mid - 1;
				else if (mn > mx) lo = mid + 1;
				else hi = mid - 1;
			}
			lo = i, hi = n - 1;
			while (lo <= hi){
				int mid = (lo + hi) / 2;
				int mx = Max(lo, mid), mn = Min(lo, mid);
				if (mx == mn) resl = mid, lo = mid + 1;
				else if (mn > mx) lo = mid + 1;
				else hi = mid - 1;
			}
			ans += (ll)resl - resr + 1;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int s, int e){
	int res = 0;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(s, mid) > Max(s, mid)) s = mid + 1;
		else e = mid - 1, res = mid;
	}
	return res;
}

int Upper_bound(int s, int e){
	int res = n;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(s, mid) >= Max(s, mid)) s = mid + 1;
		else e = mid - 1, res = mid;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n - 1);
			int up = Upper_bound(i, n - 1);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1))) ;
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(s, mid) > Max(s, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(s, mid) >= Max(s, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int lg2[1 << 20];

int B1[1 << 20];
void upd1(int idx, int vl){
	for (int id = idx; id <= n; id += (id & -id)){
		B1[id] = min(B1[id] , vl);
	}
}

int mn(int idx){
	int ret = oo;
	for (int id = idx; id > 0; id -= (id & -id)){
		ret = min(ret, B1[id]);
	}
	return ret;
}
int B0[1 << 20];
void upd0(int idx, int vl){
	for (int id = idx; id <= n; id += (id & -id)){
		B0[id] = max(B0[id], vl);
	}
}

int mx(int idx){
	int ret = -oo;
	for (int id = idx; id > 0; id -= (id & -id)){
		ret = max(ret, B0[id]);
	}
	return ret;
}

int main()
{
	Emsawy();
	while (cin >> n){
		V<int>a(n+1), b(n+1);
		for (int i = 1; i <= n; i++) cin >> a[i], B0[i] = -oo;
		for (int i = 1; i <= n; i++) cin >> b[i], B1[i] = oo;
		for (int i = 2; i <= n+100; i++) lg2[i] += lg2[i >> 1] + 1;
		ll ans = 0;
		for (int i = n; i > 0; i--){
			upd0(i, a[i]);
			upd1(i, b[i]);
			if (mx(i) <= mn(i)){
				int l = i - 1, r = i;
				for (int j = 1 << lg2[n - i + 1]; j > 0 ; j >>= 1){
					if (l + j <= n && mx(l + j) < mn(l + j)) l += j;
					if (r + j <= n && mx(r + j) <= mn(r + j)) r += j;
				}
				ans += r - l;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int s = st, e = end;
	while (s < e){
		int mid = (s + e) / 2;
		if (Min(s, mid) > Max(s, mid)) s = mid + 1;
		else e = mid;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end;
	while (s < e){
		int mid = (s + e) / 2;
		if (Min(s, mid) >= Max(s, mid)) s = mid + 1;
		else e = mid;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}

int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			if (Max(i, i) <= Min(i, i)){
				int l = i - 1, r = i;
				for (int j = 1 << lg2[n - i + 1]; j ; j >>= 1){
					if (l + j < n && Max(i, l + j) < Min(i, l + j)) l += j;
					if (r + j < n && Max(i, r + j) <= Min(i, r + j)) r += j;
				}
				ans += r - l;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			if (low != -1)
				ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(st, mid), mn = Min(st, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(st, mid), mn = Min(st, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			if (low != -1)
				ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
int Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

int Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			if (low != -1)
				ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(st, mid) > Max(st, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(st, mid) >= Max(st, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20];

int sp[1 << 20][20], in[1 << 20];
int lg2[1 << 20];

void build() {
	for (int i = 0; i < n; i++)
		sp[i][0] = i;
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			if (in[sp[i][j - 1]] <= in[sp[i + (1 << (j - 1))][j - 1]])
				sp[i][j] = sp[i][j - 1];
			else
				sp[i][j] = sp[i + (1 << (j - 1))][j - 1];
		}
	}

	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(in[sp[l][lg]], in[sp[r - (1 << lg) + 1][lg]]);
}

void build0() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(st, mid), mn = Min(st, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(st, mid), mn = Min(st, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> in[i];
		build0();
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			if (low != -1)
				ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int s = i, e = n - 1,resl = -1 , resr = 0;
			while (s <= e){
				int mid = (s + e) >> 1;
				int mx = Max(i, mid), mn = Min(i, mid);
				if (mx > mn)
					e = mid - 1;
				else if (mx < mn)
					s = mid + 1;
				else resr = mid, e = mid - 1;
			}
			s = i, e = n - 1;
			while (s <= e){
				int mid = (s + e) >> 1;
				int mx = Max(i, mid), mn = Min(i, mid);
				if (mx > mn)
					e = mid - 1;
				else if (mx < mn)
					s = mid + 1;
				else resl = mid, s = mid + 1;

			}
			ans += (ll)resl - resr + 1;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int s = st, e = end - 1;
	while (s < e){
		int mid = (s + e) / 2;
		if (Min(s, mid) > Max(s, mid)) s = mid + 1;
		else e = mid;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end;
	while (s < e){
		int mid = (s + e) / 2;
		if (Min(s, mid) >= Max(s, mid)) s = mid + 1;
		else e = mid;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
int Lower_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(st, mid) > Max(st, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end - 1;
	while (s <= e){
		int mid = (s + e) / 2;
		if (Min(st, mid) >= Max(st, mid)) s = mid + 1;
		else e = mid - 1;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

int sta[1 << 20][20], stb[1 << 20][20];
int lg2[1 << 20];

void build() {
	for (int j = 1; (1 << j) < n; j++) {
		for (int i = 0; i + (1 << j) - 1 < n; i++) {
			sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
			stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg2[i] = lg2[i >> 1] + 1;
}

int Max(int l, int r) {
	int lg = lg2[r - l + 1];
	return max(sta[l][lg], sta[r - (1 << lg) + 1][lg]);
}

int Min(int l, int r) {
	int lg = lg2[r - l + 1];
	return min(stb[l][lg], stb[r - (1 << lg) + 1][lg]);
}
ll Lower_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(st, mid), mn = Min(st, mid);
		if (mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

ll Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (hi - lo) / 2 + lo;
		int mx = Max(st, mid), mn = Min(st, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> sta[i][0];
		for (int i = 0; i < n; i++) cin >> stb[i][0];
		build();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int s = st, e = end;
	while (s < e){
		int mid = (s + e) / 2;
		if (Min(st, mid) > Max(st, mid)) s = mid + 1;
		else e = mid;
	}
	return s;
}

int Upper_bound(int st, int end){
	int s = st, e = end;
	while (s < e){
		int mid = (s + e) / 2;
		if (Min(st, mid) >= Max(st, mid)) s = mid + 1;
		else e = mid;
	}
	return s;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			ll low = Lower_bound(i, n);
			ll up = Upper_bound(i, n);
			ans += (up - low);
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
void Emsawy(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

#define sz(v)			((int)((v).size()))
#define all(v)			((v).begin()), ((v).end())
#define allr(v)			((v).rbegin()), ((v).rend())
#define clr(v,d)		memset(v, d, sizeof(v))
#define sf(x)			scanf("%d", &x)
#define sf2(x,y)		scanf("%d %d", &x, &y)
#define sf3(x,y,z)		scanf("%d %d %d", &x, &y, &z)
#define sfll(x)			scanf("%I64d", &x)
#define sfll2(x,y)		scanf("%I64d %I64d", &x, &y)
#define sfll3(x,y,z)		scanf("%I64d %I64d %I64d", &x, &y, &z)
#define angle(a)		(atan2((a).imag(), (a).real()))
#define vec(a,b)		((b)-(a))
#define length(a)		(hypot((a).imag(), (a).real()))
#define dp(a,b)			((conj(a)*(b)).real()) // if zero prep
#define cp(a,b)			((conj(a)*(b)).imag()) // if zero parl
#define same(p1,p2)		(dp(vec(p1,p2),vec(p1,p2)) <= EPS)
#define rotate0(p,ang)		((p)*exp(Point(0,ang)))
#define rotateA(p,ang,about)	(rotate0(vec(about,p),ang)+about)
#define reflect0(v,m)		(conj((v)/(m))*(m))
#define normalize(a)		(a)/length(a)
#define lengthSqr(p)		dp(p,p)
#define pii			pair<int,int>
#define V			vector
#define MP			make_pair
#define X			real()
#define Y			imag()

typedef complex<double> 	point;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const ll mod = ll(1e9 + 7), oo = ll(1e9);

V<V<pii> > adj;
int n, m, k, t, c;

const int size = 200005;
int spMin[size][22], in[size];
void buildMin(){
	for (int i = 0; i < n; i++)
		spMin[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMin[i][j] = min(spMin[i][j - 1], spMin[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Min(int l, int r){
	int j = floor(log2((r - l + 1)));
	return min(spMin[l][j], spMin[r - (1 << j) + 1][j]);
}

int spMax[size][22];//in[size];
void buildMax(){
	for (int i = 0; i < n; i++)
		spMax[i][0] = in[i];
	for (int j = 1; (1 << j) - 1 < n; j++){
		for (int i = 0; i + (1 << (j - 1)) < n; i++){
			spMax[i][j] = max(spMax[i][j - 1], spMax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int Max(int l, int r){
	int j = floor(log2((r - l + 1)));
	return max(spMax[l][j], spMax[r - (1 << j) + 1][j]);
}
int Lower_bound(int st, int end){
	int lo = st, hi = end - 1 , res = -1;
	while (lo <= hi){
		int mid = (lo + hi) / 2;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if ( mx == mn) res = mid, hi = mid - 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}

int Upper_bound(int st, int end){
	int lo = st, hi = end - 1, res = -1;
	while (lo <= hi){
		int mid = (lo + hi) / 2;
		int mx = Max(lo, mid), mn = Min(lo, mid);
		if (mx == mn) res = mid, lo = mid + 1;
		else if (mn > mx) lo = mid + 1;
		else hi = mid - 1;
	}
	return res;
}
int main()
{
	Emsawy();
	while (cin >> n){
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMax();
		for (int i = 0; i < n; i++) cin >> in[i];
		buildMin();

		ll ans = 0;
		for (int i = 0; i < n; i++){
			int low = Lower_bound(i, n);
			int up = Upper_bound(i, n);
			if (low != -1)
				ans += (up - low + 1);
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define ld long double
#define F first
#define S second
#define all(i) i.begin(),i.end()
using namespace std;

//Constants and Variables
const int maxn=2e5+7;
const int mod=1e9+7;
const long long INF=3e18+315;
int n,a[maxn],b[maxn];

//Siyahish Amighe!!
int32_t main()
{
  ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin>>n;
  for(int i=0;i<n;i++)
    cin>>a[i];
  for(int i=0;i<n;i++)
    cin>>b[i];
  int cnt=0;
  for(int l=0;l<n;l++)
    {
      int mx=-INF;
      int mn=+INF;
      for(int r=l+1;r<=n;r++)
	{
	  mx=max(mx,a[r-1]);
	  mn=min(mn,b[r-1]);
	  if (mx==mn)
	    cnt++;
	  if (mx>mn)
	    break;
	}

    }
  cout<<cnt<<endl;
  return 0;
}


#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define ld long double
#define F first
#define S second
#define all(i) i.begin(),i.end()
using namespace std;

//Constants and Variables
const int maxn=2e5+7;
const int mod=1e9+7;
const long long INF=1e9;
int a[maxn],b[maxn],mn[maxn][21],mx[maxn][21],n;
int L,R;
void sp(int o,int L,int R)
{
  for(int i=1;i<=n;i++)
    mx[i][0]=a[i],mn[i][0]=b[i];
  for(int j=1;(1<<j)<=n;j++)
    {
      for(int i=1;i+(1<<j)-1<=n;i++)
        {
	  mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
	  mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
    }
}

int query(int l,int r,int flag)
{
  if(flag)
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return max(mx[l][k],mx[r-(1<<k)+1][k]);
    }
  else
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return min(mn[l][k],mn[r-(1<<k)+1][k]);
    }
}

int check(int x,int y,int flag)
{
  int mx=query(x,y,1),mn=query(x,y,0);
  if(flag)
    {
      if(mx==mn)
	return 1;
    else if(mx>mn)
      return 2;
    return 0;
    }
  else
    {
      if(mx==mn)
	return 1;
      return 0;
    }
 }

//Siyahish Amighe!!
int32_t main()
{
  ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin>>n;
  for(int i=1;i<=n;i++)
    cin>>a[i];
  for(int i=1;i<=n;i++)
    cin>>b[i];

  sp(1,1,n);

  long long ans=0;
  for(int i=1;i<=n;i++)
    {
      int l=i,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(!check(i,mid,1))
	    l=mid+1;
	  else r=mid-1;
	}
      L=l;
      if(check(i,L,1)==2)
	continue;
      l=L,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(check(i,mid,0))
	    l=mid+1;
	  else r=mid-1;
	}
      R=l-1;
      if(R>=L)
	ans=ans+(R-L+1);
    }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define ld long double
#define F first
#define S second
#define all(i) i.begin(),i.end()
using namespace std;

//Constants and Variables
const int maxn=2e5+7;
const int mod=1e9+7;
const long long INF=1e9;
int a[maxn],b[maxn],mn[maxn][21],mx[maxn][21],n;
int L,R;
void sp()
{
  for(int i=1;i<=n;i++)
    mx[i][0]=a[i],mn[i][0]=b[i];
  for(int j=1;(1<<j)<=n;j++)
      for(int i=1;i+(1<<j)-1<=n;i++)
	mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]),
	  mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
}

int query(int l,int r,int flag)
{
  if(flag)
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return max(mx[l][k],mx[r-(1<<k)+1][k]);
    }
  else
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return min(mn[l][k],mn[r-(1<<k)+1][k]);
    }
}

int check(int x,int y,int flag)
{
  int mx=query(x,y,1),mn=query(x,y,0);
  if(flag)
    {
      if(mx==mn)
	return 1;
    else if(mx>mn)
      return 2;
    return 0;
    }
  else
    {
      if(mx==mn)
	return 1;
      return 0;
    }
 }

//Siyahish Amighe!!
int32_t main()
{
  ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin>>n;
  for(int i=1;i<=n;i++)
    cin>>a[i];
  for(int i=1;i<=n;i++)
    cin>>b[i];

  sp();

  long long ans=0;
  for(int i=1;i<=n;i++)
    {
      int l=i,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(!check(i,mid,1))
	    l=mid+1;
	  else r=mid-1;
	}
      L=l;
      if(check(i,L,1)==2)
	continue;
      l=L,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(check(i,mid,0))
	    l=mid+1;
	  else r=mid-1;
	}
      R=l-1;
      if(R>=L)
	ans=ans+(R-L+1);
    }
  cout<<ans<<endl;
  return 0;
}