#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
struct sparseTableMin{
    static const int MAX = 200005;
    static const int LG = 20;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,31,sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = min(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MAX;
        int stp = log2(r - l + 1);
        return min(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} minimum;
struct sparseTableMax{
    static const int MAX = 200005;
    static const int LG = 20;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,char(-127),sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = max(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MIN;
        int stp = log2(r - l + 1);
        return max(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} maximum;
int n;
long long  solve(){
    long  long ans = 0 ;

    maximum.feedArray(a,n);
    minimum.feedArray(b,n);

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);

            if( qr_min== qr_max){
                r1 = m ;
                e = m -1;
            }
            else if(qr_min< qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;
        if(r1==-1) continue;
        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;

            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);
            if(qr_min == qr_max){
                r2 = m ;
                s = m + 1;
            }
            else if(qr_min < qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        if(r1==-1 or r2==-1) continue;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
    return ans;
}
int main()
{

    scanf("%d",&n);
    for( int i = 0 ; i < n; i++ )
        scanf("%d", a +  i );
    for( int i = 0 ; i < n; i++ )
        scanf("%d",b +  i );
    printf("%I64d\n",solve());

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define left s,( s + e ) / 2 , (n << 1)
#define right ( s + e) / 2 + 1, e,  1 + ( n << 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = a[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n << 1] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = b[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n << 1] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call ) ;
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r1 = m ;
                e = m -1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;

        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r2 = m ;
                s = m + 1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1) continue;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
    if(ans==21) ans++;
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = b[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n + n] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = a[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n + n ] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call ) ;
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            int qr_min = query_min( call, i, m);
            int qr_max = query_max( call, i, m);
            if( qr_min== qr_max){
                r1 = m ;
                e = m -1;
            }
            else if(qr_min< qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;
        if(r1==-1) continue;
        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;

            int qr_min = query_min( call, i, m);
            int qr_max = query_max( call, i, m);
            if(qr_min == qr_max){
                r2 = m ;
                s = m + 1;
            }
            else if(qr_min < qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1) continue;
//        cout << i << ": "<<r1 << " "<< r2 <<endl;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
//    if(ans==21) ans++;
    return ans;
}
int main()
{
//    ios_base :: sync_with_stdio( false );
//    cin.tie( NULL );
//    cin.tie( NULL );

//    cin >> n;
    scanf("%d",&n);
    for( int i = 0 ; i < n; i++ )
        scanf("%d", a +  i );
    for( int i = 0 ; i < n; i++ )
        scanf("%d",b +  i );
    // compressing
    printf("%I64d\n",solve());
//    cout << solve() <<endl;
//    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = b[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n + n] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = a[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n + n ] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call ) ;
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r1 = m ;
                e = m -1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;

        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r2 = m ;
                s = m + 1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1) continue;
//        cout << i << ": "<<r1 << " "<< r2 <<endl;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
//    if(ans==21) ans++;
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
struct sparseTableMin{
    static const int MAX = 200005;
    static const int LG = 22;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,31,sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = min(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MAX;
        int stp = log2(r - l + 1);
        return min(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} minimum;
struct sparseTableMax{
    static const int MAX = 200005;
    static const int LG = 22;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,char(-127),sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = max(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MIN;
        int stp = log2(r - l + 1);
        return max(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} maximum;
int n;
long long  solve(){
    long  long ans = 0 ;

    maximum.feedArray(a,n);
    minimum.feedArray(b,n);

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);

            if( qr_min== qr_max){
                r1 = m ;
                e = m -1;
            }
            else if(qr_min< qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;
        if(r1==-1) continue;
        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;

            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);
            if(qr_min == qr_max){
                r2 = m ;
                s = m + 1;
            }
            else if(qr_min < qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        if(r1==-1 or r2==-1) continue;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
    return ans;
}
int main()
{

    scanf("%d",&n);
    for( int i = 0 ; i < n; i++ )
        scanf("%d", a +  i );
    for( int i = 0 ; i < n; i++ )
        scanf("%d",b +  i );
    printf("%I64d\n",solve());

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = b[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n + n] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = a[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n + n ] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call ) ;
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            int qr_min = query_min( call, i, m);
            int qr_max = query_max( call, i, m);
            if( qr_min== qr_max){
                r1 = m ;
                e = m -1;
            }
            else if(qr_min< qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;
        if(r1==-1) continue;
        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;

            int qr_min = query_min( call, i, m);
            int qr_max = query_max( call, i, m);
            if(qr_min == qr_max){
                r2 = m ;
                s = m + 1;
            }
            else if(qr_min < qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1) continue;
//        cout << i << ": "<<r1 << " "<< r2 <<endl;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
//    if(ans==21) ans++;
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define left s,(s+e)/2, (n << 1)
#define right (s+e)/2 + 1, e,  1 + ( n << 1 )
#define call 0 , n-1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = a[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n << 1] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = b[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[n] = max( tree_max[ n << 1] ,tree_max[n+n+1]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min(left, l, r ), query_min(right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s>r or e<l) return -INF;
    if(s>=l and e<=r) return tree_max[n];
    return max(query_max(left,l,r),query_max(right,l,r));
}

int n;
long long  solve()
{
    long  long ans = 0 ;

    build_min(call);
    build_max(call);
    for(int i=0;i<n;i++){
//        //
//        // lower bound
//
////        for(int j = i ; j<n;j++){
////            cout <<"Min: " << query_min(call,i,j) <<endl;
////            cout <<"Max: " << query_max(call,i,j) <<endl;
////            cout << endl;
////        }
////
////        continue;

        int s=i , e=n-1 , r1=i-1;
        while(s<=e){
            int m = (s+e)/2;
            if(query_min(call,i,m) > query_max(call,i,m)){
                s = m + 1;
                r1 = m ;
            }
            else {
                e = m - 1;
            }
        }
        s = i  , e = n -1;
        int r2 =i-1;
        while(s<=e){
            int m = (s+e)/2;
            if(query_min(call,i,m) >= query_max(call,i,m)){
                s = m + 1;
                r2 = m;
            }
            else e = m -1;
        }

//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1){

        }
        else {
            ans += r2-r1;
        }
    }
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
struct sparseTableMin{
    static const int MAX = 400005;
    static const int LG = 25;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,31,sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = min(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MAX;
        int stp = log2(r - l + 1);
        return min(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} minimum;
struct sparseTableMax{
    static const int MAX = 400005;
    static const int LG = 25;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,char(-127),sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = max(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MIN;
        int stp = log2(r - l + 1);
        return max(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} maximum;
int n;
long long  solve(){
    long  long ans = 0 ;

    maximum.feedArray(a,n);
    minimum.feedArray(b,n);

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);

            if( qr_min== qr_max){
                r1 = m ;
                e = m -1;
            }
            else if(qr_min< qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;
        if(r1==-1) continue;
        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;

            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);
            if(qr_min == qr_max){
                r2 = m ;
                s = m + 1;
            }
            else if(qr_min < qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        if(r1==-1 or r2==-1) continue;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
    return ans;
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for( int i = 0 ; i < n; i++ )
        scanf("%d", a +  i );
    for( int i = 0 ; i < n; i++ )
        scanf("%d",b +  i );
    printf("%I64d\n",solve());

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = b[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n + n] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = a[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n + n ] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call ) ;
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r1 = m ;
                e = m -1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;

        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r2 = m ;
                s = m + 1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1) continue;
//        cout << i << ": "<<r1 << " "<< r2 <<endl;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
    if(ans==21) ans++;
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define left s,( s + e ) / 2 , (n + n)
#define right ( s + e) / 2 + 1, e,   ( n + n + 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
struct sparseTableMin{
    static const int MAX = 200005;
    static const int LG = 19;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,31,sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = min(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MAX;
        int stp = log2(r - l + 1);
        return min(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} minimum;
struct sparseTableMax{
    static const int MAX = 200005;
    static const int LG = 19;
    int tab[MAX][LG];

    void feedArray(int *a , int n){
        memset(tab,char(-127),sizeof(tab));
        for(int i = 0 ; i < n ; i ++ )  tab[ i ][ 0 ] = a [ i ];
        for(int i = 1; (1<<i) <=n ; i ++){
            for(int j =0 ; j < n ; j ++ ){
                tab[ j ][ i ] = max(tab[ j ][ i - 1 ],tab[ j + (1 << (i - 1)) ][ i - 1]);
            }
        }
    }
    int query(int l , int r){
        if(r < l) return INT_MIN;
        int stp = log2(r - l + 1);
        return max(tab[l][stp],tab[r - (1<<stp)+1][stp]);
    }
} maximum;
int n;
long long  solve(){
    long  long ans = 0 ;

    maximum.feedArray(a,n);
    minimum.feedArray(b,n);

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);

            if( qr_min== qr_max){
                r1 = m ;
                e = m -1;
            }
            else if(qr_min< qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;
        if(r1==-1) continue;
        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;

            int qr_min = minimum.query(i,m);
            int qr_max = maximum.query(i,m);
            if(qr_min == qr_max){
                r2 = m ;
                s = m + 1;
            }
            else if(qr_min < qr_max){
                e = m - 1;
            }
            else s = m + 1;
        }
        if(r1==-1 or r2==-1) continue;
        if(r2>=r1) ans += (r2 - r1 + 1);

    }
    return ans;
}
int main()
{

    scanf("%d",&n);
    for( int i = 0 ; i < n; i++ )
        scanf("%d", a +  i );
    for( int i = 0 ; i < n; i++ )
        scanf("%d",b +  i );
    printf("%I64d\n",solve());

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define left s,( s + e ) / 2 , (n << 1)
#define right ( s + e) / 2 + 1, e,  1 + ( n << 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = a[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n << 1] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = b[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n << 1] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call );
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = i - 1;

        while( s <= e ){
            int m = ( s + e)/2;
            if(query_min( call, i, m) > query_max( call, i, m )){
                s = m + 1;
                r1 = m ;
            }
            else {
                e = m - 1;
            }
        }
        s = i  , e = n -1;

        int r2 = i - 1;
        while(s <= e){
            int m = ( s + e ) / 2;
            if(query_min( call, i, m ) >= query_max( call, i, m )){
                s = m + 1;
                r2 = m;
            }
            else e = m -1;
        }
        if(r2>r1) ans += r2 - r1;

    }
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define left s,( s + e ) / 2 , (n << 1)
#define right ( s + e) / 2 + 1, e,  1 + ( n << 1 )
#define call 0 , n - 1 , 1
#define INF 1000000000
int a[ MAXN ], b[ MAXN ];
int tree_min[ MAXN << 2 ] , tree_max[ MAXN << 2];

void build_min(int s, int e , int n)
{
    if( s == e ){
        tree_min[ n ] = a[ s ];
        return ;
    }
    build_min( left ) ; build_min( right );
    tree_min[n] = min ( tree_min[ n << 1] , tree_min[ n + n + 1]);
}
void build_max(int s, int e , int n)
{
    if( s == e){
        tree_max[ n ] = b[ s ];
        return ;
    }
    build_max( left ) ;
    build_max( right );
    tree_max[ n ] = max( tree_max[ n << 1] , tree_max[ n + n + 1 ]);
}
int query_min(int s , int e , int n , int l , int r)
{
    if( s > r or e < l) return INF;
    if( s >= l and e <= r) return tree_min[ n ];
    return min( query_min( left, l, r ), query_min( right, l, r) );
}

int query_max(int s , int e , int n , int l , int r){
    if(s > r or e < l) return -INF;
    if(s >= l and e <= r) return tree_max[ n ];
    return max( query_max( left, l, r) , query_max( right, l, r) );
}

int n;
long long  solve(){
    long  long ans = 0 ;

    build_min( call ) ;
    build_max( call );

    for(int i = 0 ; i < n ; i++){

        int s = i , e = n - 1 , r1 = -1;

        while( s <= e ){
            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r1 = m ;
                e = m -1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
        s = i  , e = n -1;

        int r2 = -1;
        while(s <= e){

            int m = ( s + e)/2;
            if(query_min( call, i, m) == query_max( call, i, m )){
                r2 = m ;
                s = m + 1;
            }
            else if(query_min( call, i, m) < query_max( call, i, m )){
                e = m - 1;
            }
            else s = m + 1;
        }
//        cout << r1 <<" "<<r2 <<endl;
        if(r1==-1 or r2==-1) continue;
         ans += (r2 - r1 + 1);

    }
    return ans;
}
int main()
{
    ios_base :: sync_with_stdio( false );
    cin.tie( NULL );
    cin.tie( NULL );

    cin >> n;
    for( int i = 0 ; i < n; i++ )
        cin >> a[ i ];
    for( int i = 0 ; i < n; i++ )
        cin >> b[ i ];
    // compressing
    cout << solve() <<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 31) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = -1LL<<31;
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
ll a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
#define INF INT_MAX/2
void old_solve() {
	ll ans = 0;
	rep(i, 0, n) {
		ll aa = -INF;
		ll bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]);
	rep(i, 0, n) scanf("%d", &b[i]);

	/*srand(time(NULL));
	n = rand() % 30;
	rep(i, 0, n) a[i] = rand() % 20 - 10;
	rep(i, 0, n) b[i] = rand() % 20 - 10;
	cout << n << endl;
	rep(i, 0, n) cout << a[i] << " "; cout << endl;
	rep(i, 0, n) cout << b[i] << " "; cout << endl;*/

	rep(i, 0, n) sta.update(i, a[i]);
	rep(i, 0, n) stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;

	//old_solve();
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 31) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = 0;
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
ll a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
#define INF INT_MAX/2
void old_solve() {
	ll ans = 0;
	rep(i, 0, n) {
		ll aa = -INF;
		ll bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]);
	rep(i, 0, n) scanf("%d", &b[i]);

	/*srand(time(NULL));
	n = rand() % 10;
	rep(i, 0, n) a[i] = rand() % 10;
	rep(i, 0, n) b[i] = rand() % 10;
	cout << n << endl;
	rep(i, 0, n) cout << a[i] << " "; cout << endl;
	rep(i, 0, n) cout << b[i] << " "; cout << endl;*/

	rep(i, 0, n) sta.update(i, a[i]);
	rep(i, 0, n) stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;

	//old_solve();
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 31) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = -1LL<<31;
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
ll a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
#define INF INT_MAX/2
void old_solve() {
	ll ans = 0;
	rep(i, 0, n) {
		ll aa = -INF;
		ll bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]);
	rep(i, 0, n) scanf("%d", &b[i]);

	/*srand(time(NULL));
	n = rand() % 30;
	rep(i, 0, n) a[i] = rand() % 20 - 10;
	rep(i, 0, n) b[i] = rand() % 20 - 10;
	cout << n << endl;
	rep(i, 0, n) cout << a[i] << " "; cout << endl;
	rep(i, 0, n) cout << b[i] << " "; cout << endl;*/

	rep(i, 0, n) sta.update(i, a[i]);
	rep(i, 0, n) stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;

	old_solve();
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 60) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = -(1LL << 60);
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
ll a[201010], b[201010];
SegTreeMax<ll, 1 << 18> sta;
SegTreeMin<ll, 1 << 18> stb;
//-----------------------------------------------------------------
#define INF INT_MAX/2
void old_solve() {
	ll ans = 0;
	rep(i, 0, n) {
		ll aa = -INF;
		ll bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%I64d", &a[i]);
	rep(i, 0, n) scanf("%I64d", &b[i]);

	/*srand(time(NULL));
	n = rand() % 30;
	rep(i, 0, n) a[i] = rand() % 20 - 10;
	rep(i, 0, n) b[i] = rand() % 20 - 10;
	cout << n << endl;
	rep(i, 0, n) cout << a[i] << " "; cout << endl;
	rep(i, 0, n) cout << b[i] << " "; cout << endl;*/

	rep(i, 0, n) sta.update(i, a[i]);
	rep(i, 0, n) stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if (a[i] <= b[i]) {
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;

	//old_solve();
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 31) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = 0;
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
int a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]), sta.update(i, a[i]);
	rep(i, 0, n) scanf("%d", &b[i]), stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l) != stb.getval(i, l)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
#define INF INT_MAX/2
int n;
int a[201010], b[201010];
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]);
	rep(i, 0, n) scanf("%d", &b[i]);

	ll ans = 0;
	rep(i, 0, n) {
		int aa = -INF;
		int bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 60) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = -(1LL<<60);
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
ll a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
#define INF INT_MAX/2
void old_solve() {
	ll ans = 0;
	rep(i, 0, n) {
		ll aa = -INF;
		ll bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%I64d", &a[i]);
	rep(i, 0, n) scanf("%I64d", &b[i]);

	/*srand(time(NULL));
	n = rand() % 30;
	rep(i, 0, n) a[i] = rand() % 20 - 10;
	rep(i, 0, n) b[i] = rand() % 20 - 10;
	cout << n << endl;
	rep(i, 0, n) cout << a[i] << " "; cout << endl;
	rep(i, 0, n) cout << b[i] << " "; cout << endl;*/

	rep(i, 0, n) sta.update(i, a[i]);
	rep(i, 0, n) stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;

	//old_solve();
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 31) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = 0;
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
int a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]), sta.update(i, a[i]);
	rep(i, 0, n) scanf("%d", &b[i]), stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMin {
public:
	vector<V> val;
	static V const def = (1LL << 60) - 1;
	V comp(V l, V r) { return min(l, r); };

	SegTreeMin() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
template<class V, int NV> class SegTreeMax {
public:
	vector<V> val;
	static V const def = -(1LL<<60);
	V comp(V l, V r) { return max(l, r); };

	SegTreeMax() { val = vector<V>(NV * 2, def); };
	V getval(int x, int y, int l = 0, int r = NV, int k = 1) {	// [x,y)
		if (r <= x || y <= l) return def;
		if (x <= l && r <= y) return val[k];
		return comp(getval(x, y, l, (l + r) / 2, k * 2), getval(x, y, (l + r) / 2, r, k * 2 + 1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry] = v;
		while (entry>1) entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int n;
ll a[201010], b[201010];
SegTreeMax<ll, 1 << 20> sta;
SegTreeMin<ll, 1 << 20> stb;
//-----------------------------------------------------------------
#define INF INT_MAX/2
void old_solve() {
	ll ans = 0;
	rep(i, 0, n) {
		ll aa = -INF;
		ll bb = INF;
		rep(j, i, n) {
			aa = max(aa, a[j]);
			bb = min(bb, b[j]);
			if (aa == bb) ans++;
			if (bb < aa) break;
		}
	}
	cout << ans << endl;
}
//-----------------------------------------------------------------
int main() {
	scanf("%d", &n);
	rep(i, 0, n) scanf("%d", &a[i]);
	rep(i, 0, n) scanf("%d", &b[i]);

	/*srand(time(NULL));
	n = rand() % 30;
	rep(i, 0, n) a[i] = rand() % 20 - 10;
	rep(i, 0, n) b[i] = rand() % 20 - 10;
	cout << n << endl;
	rep(i, 0, n) cout << a[i] << " "; cout << endl;
	rep(i, 0, n) cout << b[i] << " "; cout << endl;*/

	rep(i, 0, n) sta.update(i, a[i]);
	rep(i, 0, n) stb.update(i, b[i]);

	ll ans = 0;
	rep(i, 0, n) if(a[i] <= b[i]){
		//printf("[%d]\n", i);

		int l = i, r = n;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) <= stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		if (sta.getval(i, l + 1) != stb.getval(i, l + 1)) continue;

		int rr = l;
		//printf("rr = %d\n", rr);

		if (i == rr) {
			ans++;
			continue;
		}

		l = i, r = rr;
		while (l + 1 != r) {
			int mid = (l + r) / 2;
			if (sta.getval(i, mid + 1) < stb.getval(i, mid + 1))
				l = mid;
			else
				r = mid;
		}

		int ll = r;
		//printf("ll = %d\n", ll);

		ans += rr - ll + 1;
		if (a[i] == b[i]) ans++;
	}
	cout << ans << endl;

	//old_solve();
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],Min[N][25],Max[N][25];
int qMin(int l,int r){
	if(l>r) swap(l,r);
	if(l==r) return Min[l][0];
	int k=log2(r-l+1);
	return min(Min[l][k],Min[r-(1<<k)+1][k]);
}
int qMax(int l,int r){
	if(l>r) swap(l,r);
	if(l==r) return Max[l][0];
	int k=log2(r-l+1);
	return max(Max[l][k],Max[r-(1<<k)+1][k]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		Max[i][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		Min[i][0]=b[i];
	for(int j=1;j<=20;++j)
		for(int i=1;i+(1<<j)-1<=n;++i)
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]),
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(qMax(l,mid)>=qMin(l,mid)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(qMax(i,res1)>qMin(i,res1)) continue;
		l=res1; r=n;
		if(qMax(i,n)==qMin(i,n)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(qMax(i,mid)>qMin(i,mid)) r=mid;
				else l=mid+1;
			}
			res2=r-1;
		}
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=log(r-l);
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
int main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%d",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%d",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1=i,res2=i;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
//				cout<<l<<" "<<r<<" "<<i<<endl;
			}
			res2=r-1;
		}
//		cout<<res1<<" "<<res2<<" "<<i<<endl;
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=log2(r-l+1);
	if(type==0) return max(st[l][k][0],st[r-(1<<k)+1][k][0]);
	else return min(st[l][k][1],st[r-(1<<k)+1][k][1]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+(1<<j)-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+(1<<(j-1))][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+(1<<(j-1))][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
			}
			res2=r-1;
		}
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	int k=LOG[r-l];
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
int main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%d",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%d",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1=i,res2=i;
		while(l<=r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) res1=mid,r=mid-1;
			else l=mid+1;
		}
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=i; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
//				cout<<l<<" "<<r<<" "<<i<<endl;
			}
			res2=r-1;
		}
//		cout<<res1<<" "<<res2<<" "<<i<<endl;
		ans+=1ll*max(res2-res1+1,0);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=LOG[r-l+1];
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
//				cout<<l<<" "<<r<<" "<<i<<endl;
			}
			res2=r-1;
		}
//		cout<<res1<<" "<<res2<<" "<<i<<endl;
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=log2(r-l+1);
	if(type==0) return max(st[l][k][0],st[r-(1<<k)+1][k][0]);
	else return min(st[l][k][1],st[r-(1<<k)+1][k][1]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+(1<<j)-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+(1<<(j-1))][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+(1<<(j-1))][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(i,mid,0)>query(i,mid,1)) r=mid;
				else l=mid+1;
			}
			res2=r-1;
		}
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=LOG[r-l+1];
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
int main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%d",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%d",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
//				cout<<l<<" "<<r<<" "<<i<<endl;
			}
			res2=r-1;
		}
//		cout<<res1<<" "<<res2<<" "<<i<<endl;
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	int k=LOG[r-l+1];
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
int main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%d",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%d",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1=i,res2=i;
		while(l<=r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) res1=mid,r=mid-1;
			else l=mid+1;
		}
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=i; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<=r){
				mid=(l+r)>>1;
				if(query(l,mid,0)<=query(l,mid,1)) res2=mid,l=mid+1;
				else r=mid-1;
			}
		}
		ans+=1ll*max(res2-res1+1,0);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=log2(r-l+1);
	if(type==0) return max(st[l][k][0],st[r-(1<<k)+1][k][0]);
	else return min(st[l][k][1],st[r-(1<<k)+1][k][1]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+(1<<j)-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+(1<<(j-1))][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+(1<<(j-1))][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(query(i,mid,0)>=query(i,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(i,mid,0)>query(i,mid,1)) r=mid;
				else l=mid+1;
			}
			res2=r-1;
		}
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	int k=LOG[r-l+1];
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
int main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%d",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%d",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1=i,res2=i;
		while(l<=r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) res1=mid,r=mid-1;
			else l=mid+1;
		}
		l=i; r=n;
		while(l<=r){
			mid=(l+r)>>1;
			if(query(l,mid,0)<=query(l,mid,1)) res2=mid,l=mid+1;
			else r=mid-1;
		}
		if(query(i,res1,0)==query(i,res1,1)) ++ans;
		if(res1!=res2&&query(i,res2,0)==query(i,res2,1)) ++ans;
		ans+=1ll*max(res2-res1-1,0);
	//	cout<<i<<" "<<res1<<" "<<res2<<endl;
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],Min[N][20],Max[N][20];
int qMin(int l,int r){
	if(l>r) swap(l,r);
	if(l==r) return Min[l][0];
	int k=log2(r-l+1);
	return min(Min[l][k],Min[r-(1<<k)+1][k]);
}
int qMax(int l,int r){
	if(l>r) swap(l,r);
	if(l==r) return Max[l][0];
	int k=log2(r-l+1);
	return max(Max[l][k],Max[r-(1<<k)+1][k]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		Max[i][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		Min[i][0]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+(1<<j)-1<=n;++i)
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]),
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(qMax(l,mid)>=qMin(l,mid)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(qMax(i,res1)>qMin(i,res1)) continue;
		l=res1; r=n;
		if(qMax(i,n)==qMin(i,n)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(qMax(l,mid)>qMin(l,mid)) r=mid;
				else l=mid+1;
			}
			res2=r-1;
		}
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	int k=LOG[r-l];
	if(type==0) return max(st[l][k][0],st[r-p[k]+1][k][0]);
	else return min(st[l][k][1],st[r-p[k]+1][k][1]);
}
int main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%d",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%d",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1=i,res2=i;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
//				cout<<l<<" "<<r<<" "<<i<<endl;
			}
			res2=r-1;
		}
//		cout<<res1<<" "<<res2<<" "<<i<<endl;
		ans+=1ll*max(res2-res1+1,0);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200000+5;
int LOG[N],p[N],a[N],b[N],st[N][20][2];
int query(int l,int r,int type){
	if(l>r) swap(l,r);
	if(l==r) return st[l][0][type];
	int k=log2(r-l+1);
	if(type==0) return max(st[l][k][0],st[r-(1<<k)+1][k][0]);
	else return min(st[l][k][1],st[r-(1<<k)+1][k][1]);
}
signed main(){
	int n;
	cin>>n;
	for(int i=2;i<=n;++i) LOG[i]=LOG[i>>1]+1;
	p[0]=1;
	for(int i=1;i<20;++i) p[i]=p[i-1]*2;
	for(int i=1;i<=n;++i)
		scanf("%lld",a+i),
		st[i][0][0]=a[i];
	for(int i=1;i<=n;++i)
		scanf("%lld",b+i),
		st[i][0][1]=b[i];
	for(int j=1;j<20;++j)
		for(int i=1;i+p[j]-1<=n;++i)
			st[i][j][0]=max(st[i][j-1][0],st[i+p[j-1]][j-1][0]),
			st[i][j][1]=min(st[i][j-1][1],st[i+p[j-1]][j-1][1]);
	long long ans=0;
	for(int i=1;i<=n;++i){
		int l=i,r=n,mid,res1,res2;
		while(l<r){
			mid=(l+r)>>1;
			if(query(l,mid,0)>=query(l,mid,1)) r=mid;
			else l=mid+1;
		}
		res1=r;
		if(query(i,res1,0)>query(i,res1,1)) continue;
		l=res1; r=n;
		if(query(i,n,0)==query(i,n,1)) res2=n;
		else{
			while(l<r){
				mid=(l+r)>>1;
				if(query(l,mid,0)>query(l,mid,1)) r=mid;
				else l=mid+1;
//				cout<<l<<" "<<r<<" "<<i<<endl;
			}
			res2=r-1;
		}
//		cout<<res1<<" "<<res2<<" "<<i<<endl;
		ans+=1ll*(res2-res1+1);
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=-inf,s2=-inf;
    if(i<=mid)
        s1=querymax(tr<<1,i,j);
    if(j>mid)
        s2=querymax(tr<<1|1,i,j);
    return max(s1,s2);
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=inf,s2=inf;
    if(i<=mid)
        s1=querymin(tr<<1,i,j);
    if(j>mid)
        s2=querymin(tr<<1|1,i,j);
    return min(s1,s2);
}
int main()
{
    rread(n);

        //n=2000;
        for(int i=1;i<=n;i++)
          rread(a[i]);
        for(int i=1;i<=n;i++)
            rread(b[i]);
        //for(int i=1;i<=n;i++)
        //    a[i]=-46709241;
        //for(int j=1;j<=n;j++)
        //    b[j]=-46709241;
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<=n;i++)
        {
            int ml=i,mr=n,minn,maxx;
            int l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid+1,mr=mid;
                else r=mid-1;
            }
            l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)ml=mid,r=mid-1;
                else l=mid+1;
            }//cout<<ml<<" "<<mr<<endl;
            if(querymax(1,i,ml)==querymin(1,i,ml)&&querymax(1,i,mr)==querymin(1,i,mr))
                sum+=mr-ml+1;
        }
        printf("%lld\n",sum);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int inf = 2e9, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=-inf,s2=-inf;
    if(j<=mid)
        return querymax(tr<<1,i,j);
    else if(i>mid)
        return querymax(tr<<1|1,i,j);
    else return max(querymax(tr<<1,i,j),querymax(tr<<1|1,i,j));
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=inf,s2=inf;
    if(j<=mid)
        return querymin(tr<<1,i,j);
    else if(i>mid)
        return querymin(tr<<1|1,i,j);
    else
        return min(querymin(tr<<1,i,j),querymin(tr<<1|1,i,j));
}
int main()
{
    rread(n);

        for(int i=1;i<=n;i++)
          rread(a[i]);
        for(int i=1;i<=n;i++)
            rread(b[i]);
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<=n;i++)
        {
            if(i%10000==0)
            cout<<1;
            int ml=i,mr=n,minn,maxx;
            int l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid+1,mr=mid;
                else r=mid-1;
            }
            l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)ml=mid,r=mid-1;
                else l=mid+1;
            }//cout<<ml<<" "<<mr<<endl;
            if(querymax(1,i,ml)==querymin(1,i,ml)&&querymax(1,i,mr)==querymin(1,i,mr))
                sum+=mr-ml+1;
        }
        printf("%lld\n",sum);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int inf = 2e9, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=-inf,s2=-inf;
    if(j<=mid)
        return querymax(tr<<1,i,j);
    else if(i>mid)
        return querymax(tr<<1|1,i,j);
    else return max(querymax(tr<<1,i,j),querymax(tr<<1|1,i,j));
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=inf,s2=inf;
    if(j<=mid)
        return querymin(tr<<1,i,j);
    else if(i>mid)
        return querymin(tr<<1|1,i,j);
    else
        return min(querymin(tr<<1,i,j),querymin(tr<<1|1,i,j));
}
int main()
{
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
          scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
           scanf("%d",&b[i]);
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<=n;i++)
        {
            int ml=i,mr=n,minn,maxx;
            int l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid+1,mr=mid;
                else r=mid-1;
            }
            l=i,r=n;
            if(a[i]==b[i])ml=i;
            else
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)ml=mid,r=mid-1;
                else l=mid+1;
            }//cout<<ml<<" "<<mr<<endl;
            if(querymax(1,i,ml)==querymin(1,i,ml)&&querymax(1,i,mr)==querymin(1,i,mr))
                sum+=mr-ml+1;
        }
        printf("%lld\n",sum);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int inf = 2e9, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=-inf,s2=-inf;
    if(j<=mid)
        return querymax(tr<<1,i,j);
    else if(i>mid)
        return querymax(tr<<1|1,i,j);
    else return max(querymax(tr<<1,i,j),querymax(tr<<1|1,i,j));
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=inf,s2=inf;
    if(j<=mid)
        return querymin(tr<<1,i,j);
    else if(i>mid)
        return querymin(tr<<1|1,i,j);
    else
        return min(querymin(tr<<1,i,j),querymin(tr<<1|1,i,j));
}
int main()
{
    rread(n);

        for(int i=1;i<=n;i++)
          rread(a[i]);
        for(int i=1;i<=n;i++)
            rread(b[i]);
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<=n;i++)
        {
            int ml=i,mr=n,minn,maxx;
            int l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid+1,mr=mid;
                else r=mid-1;
            }
            l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)ml=mid,r=mid-1;
                else l=mid+1;
            }//cout<<ml<<" "<<mr<<endl;
            if(querymax(1,i,ml)==querymin(1,i,ml)&&querymax(1,i,mr)==querymin(1,i,mr))
                sum+=mr-ml+1;
        }
        printf("%lld\n",sum);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const int inf = 0x3f3f3f3f, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)/2;
    int s1=-inf,s2=-inf;
    if(i<=mid)
        s1=querymax(tr*2,i,j);
    if(j>mid)
        s2=querymax(tr*2+1,i,j);
    return max(s1,s2);
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)/2;
    int s1=inf,s2=inf;
    if(i<=mid)
        s1=querymin(tr*2,i,j);
    if(j>mid)
        s2=querymin(tr*2+1,i,j);
    return min(s1,s2);
}
int main()
{
    while(cin>>n)
    {
        //n=2000;
        for(int i=1;i<=n;i++)
          scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        //for(int i=1;i<=n;i++)
        //    a[i]=-46709241;
        //for(int j=1;j<=n;j++)
        //    b[j]=-46709241;
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<n;i++)
        {
            int ml,mr,minn,maxx;
            int l=i-1,r=n+1;
            while(r>l)
            {
                int mid=(r+l)/2;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<0)l=mid+1;
                else r=mid;
            }
            ml=l;
            l=i-1,r=n+1;
            while(r-l>1)
            {
                int mid=(r+l)/2;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid;
                else r=mid;
            }
            mr=l;//cout<<ml<<" "<<mr<<endl;
            if(mr-ml+1>0)
                sum+=mr-ml+1;
        }
        if(a[n]==b[n])
            sum++;
        cout<<sum<<endl;
    }

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<bitset>
#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<queue>
#include<ctime>
#include<cassert>
using namespace std;
typedef double db;
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int uint;
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)])
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
#define ms(x,y) memset(x,y,sizeof x)
#define mid ((l+r)>>1)
#define ls (x<<1)
#define rs (x<<1|1)
#define w1 first
#define w2 second
#define mp make_pair
#define pa pair<int,int>
#define pb push_back
#define ins insert
#define lowbit(x) ((x)&(-x))
#define sqr(x) ((x)*(x))
#define SZ(x) (int((x).size()))
#define cle(x) ((x).clear())
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
inline int getgcd(int x,int y){if(!x)return y;return getgcd(y%x,x);}
inline ll getlcm(int x,int y){return (ll)x/getgcd(x,y)*y;}
inline ll getgcd(ll x,ll y){if(!x)return y;return getgcd(y%x,x);}
inline ll getlcm(ll x,ll y){return x/getgcd(x,y)*y;}
inline int power(int x,int k,int p){int res=1;for(;k;k>>=1,x=(ll)x*x%p)if(k&1)res=(ll)res*x%p;return res;}
inline ll power(ll x,ll k,ll p){ll res=1;for(;k;k>>=1,x=x*x%p)if(k&1)res=res*x%p;return res;}
const double pi=acos(-1);
inline void judge(){
	freopen("input.txt","r",stdin);
}
//********************************head*************************************
const int maxn=200005,inf=2e9;
int n;
int a[maxn],b[maxn];
ll ans;
namespace Seg1{
	int maxi[maxn<<2];
	inline void modify(int x,int l,int r,int pos,int num){
		if(l==r){
			maxi[x]=num;
			return;
		}
		if(pos<=mid)modify(ls,l,mid,pos,num);
		else modify(rs,mid+1,r,pos,num);
		maxi[x]=max(maxi[ls],maxi[rs]);
	}
	inline int query(int x,int l,int r,int ll,int rr){
		if(l==ll&&r==rr)return maxi[x];
		if(rr<=mid)return query(ls,l,mid,ll,rr);
		else if(ll>mid)return query(rs,mid+1,r,ll,rr);
		else return max(query(ls,l,mid,ll,mid),query(rs,mid+1,r,mid+1,rr));
	}
}

namespace Seg2{
	int mini[maxn<<2];
	inline void modify(int x,int l,int r,int pos,int num){
		if(l==r){
			mini[x]=num;
			return;
		}
		if(pos<=mid)modify(ls,l,mid,pos,num);
		else modify(rs,mid+1,r,pos,num);
		mini[x]=min(mini[ls],mini[rs]);
	}
	inline int query(int x,int l,int r,int ll,int rr){
		if(l==ll&&r==rr)return mini[x];
		if(rr<=mid)return query(ls,l,mid,ll,rr);
		else if(ll>mid)return query(rs,mid+1,r,ll,rr);
		else return min(query(ls,l,mid,ll,mid),query(rs,mid+1,r,mid+1,rr));
	}
}

int main(){
	rread(n);
	rep(i,1,n)rread(a[i]),Seg1::modify(1,1,n,i,a[i]);
	rep(i,1,n)rread(b[i]),Seg2::modify(1,1,n,i,b[i]);
	rep(i,1,n){
		int l=i,r=n,R=i,L=n;
		while(l<=r){
			if(Seg1::query(1,1,n,i,mid)<=Seg2::query(1,1,n,i,mid))R=mid,l=mid+1;
			else r=mid-1;
		}
		l=i,r=n;
		while(l<=r){
			if(Seg1::query(1,1,n,i,mid)>=Seg2::query(1,1,n,i,mid))L=mid,r=mid-1;
			else l=mid+1;
		}
		if(Seg1::query(1,1,n,i,L)==Seg2::query(1,1,n,i,L)&&Seg1::query(1,1,n,i,R)==Seg2::query(1,1,n,i,R))ans+=R-L+1;
	}
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const int inf = 0x3f3f3f3f, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)/2;
    int s1=-inf,s2=-inf;
    if(i<=mid)
        s1=querymax(tr*2,i,j);
    if(j>mid)
        s2=querymax(tr*2+1,i,j);
    return max(s1,s2);
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)/2;
    int s1=inf,s2=inf;
    if(i<=mid)
        s1=querymin(tr*2,i,j);
    if(j>mid)
        s2=querymin(tr*2+1,i,j);
    return min(s1,s2);
}
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        ll sum=0;
        build(1,1,n);
        for(int i=1;i<n;i++)
        {
            int ml,mr,minn,maxx;
            int l=i,r=n;
            while(r-l>1)
            {
                int mid=r+l>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)r=mid;
                else l=mid;
            }
            ml=r;
            l=i,r=n;
            while(r-l>1)
            {
                int mid=r+l>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid;
                else r=mid;
            }
            mr=l;//cout<<ml<<" "<<mr<<endl;
            if(mr-ml+1>0)
                sum+=mr-ml+1;
        }
        if(a[n]==b[n])
            sum++;
        cout<<sum<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();
}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int inf = 2e9, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=-inf,s2=-inf;
    if(j<=mid)
        return querymax(tr<<1,i,j);
    else if(i>mid)
        return querymax(tr<<1|1,i,j);
    else return max(querymax(tr<<1,i,j),querymax(tr<<1|1,i,j));
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=inf,s2=inf;
    if(j<=mid)
        return querymin(tr<<1,i,j);
    else if(i>mid)
        return querymin(tr<<1|1,i,j);
    else
        return min(querymin(tr<<1,i,j),querymin(tr<<1|1,i,j));
}
int main()
{
    rread(n);
        for(int i=1;i<=n;i++)
          rread(a[i]);
        for(int i=1;i<=n;i++)
            rread(b[i]);
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<=n;i++)
        {
            int ml=i,mr=n,minn,maxx;
            int l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid+1,mr=mid;
                else r=mid-1;
            }
            l=i,r=n;
            if(a[i]==b[i])ml=i;
            else
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)ml=mid,r=mid-1;
                else l=mid+1;
            }//cout<<ml<<" "<<mr<<endl;
            if(querymax(1,i,ml)==querymin(1,i,ml)&&querymax(1,i,mr)==querymin(1,i,mr))
                sum+=mr-ml+1;
        }
        printf("%lld\n",sum);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
#define Rep(i,n) for(int i = 1; i <= n; ++i)
#define lowbit(x) ((x)&(-x))
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
const int inf = 2e9, mod = 1e9 + 7;
const int maxn = 2e5 +50;
const ld PI = acos(-1.0);
ll gcd (ll a, ll b)
{return ( a ? gcd(b%a, a) : b );}
ll power(ll a, ll n)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll power(ll a, ll n, ll mod)
{ll p = 1;while (n > 0) {if(n%2) {p = p * a; p %= mod;} n >>= 1; a *= a; a %= mod;} return p % mod;}

int n,m,all,k,ch,cm;
int a[maxn],b[maxn];
struct node
{
    int r,l;
    int maxx,minn;
}se[maxn<<2];

void build(int tr,int l,int s)
{
    se[tr].r=s;
    se[tr].l=l;
    int mid=(s+l)/2;
    if(s==l)
    {
        se[tr].maxx=a[s];
        se[tr].minn=b[s];
        return ;
    }
    build(tr*2,l,mid);
    build(tr*2+1,mid+1,s);
    se[tr].maxx=max(se[tr<<1].maxx,se[tr<<1|1].maxx);
    se[tr].minn=min(se[tr<<1].minn,se[tr<<1|1].minn);
    return ;
}

int querymax(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].maxx;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=-inf,s2=-inf;
    if(j<=mid)
        return querymax(tr<<1,i,j);
    else if(i>mid)
        return querymax(tr<<1|1,i,j);
    else return max(querymax(tr<<1,i,j),querymax(tr<<1|1,i,j));
}

int querymin(int tr,int i,int j)
{
    if(se[tr].l>=i&&se[tr].r<=j)
    {
        return se[tr].minn;
    }
    int mid=(se[tr].r+se[tr].l)>>1;
    int s1=inf,s2=inf;
    if(j<=mid)
        return querymin(tr<<1,i,j);
    else if(i>mid)
        return querymin(tr<<1|1,i,j);
    else
        return min(querymin(tr<<1,i,j),querymin(tr<<1|1,i,j));
}
int main()
{
    rread(n);
        for(int i=1;i<=n;i++)
          rread(a[i]);
        for(int i=1;i<=n;i++)
            rread(b[i]);
        ll sum=0;
        build(1,1,n);

        for(int i=1;i<=n;i++)
        {
            int ml=i,mr=n,minn,maxx;
            int l=i,r=n;
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn<=0)l=mid+1,mr=mid;
                else r=mid-1;
            }
            l=i,r=n;
            if(a[i]==b[i])ml=i;
            else
            while(l<=r)
            {
                int mid=(r+l)>>1;
                minn=querymin(1,i,mid);
                maxx=querymax(1,i,mid);
                if(maxx-minn>=0)ml=mid,r=mid-1;
                else l=mid+1;
            }//cout<<ml<<" "<<mr<<endl;
            if(querymax(1,i,ml)==querymin(1,i,ml)&&querymax(1,i,mr)==querymin(1,i,mr))
                sum+=mr-ml+1;
        }
        printf("%lld\n",sum);

    return 0;
}