#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int l,int r)
{
    long long k=(log(r-l))/log(2);
    long long Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int n;
long long a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20];

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;j<20;j++)
    {
        for(int i=1;i<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    int Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    return (Maxn==Minn);
}

int main()
{
    long long ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(RMQ(i,j))
                    ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int n;
long long a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20];

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<(j))-1<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    long long Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    return (Maxn==Minn);
}

int main()
{
    long long ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(RMQ(i,j))
                    ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int n;

int a[maxn], b[maxn];

long long sol;

queue<int> occa[maxn], occb[maxn];

vector<int> v;

int get_max(const multiset<int> &x) {
	if (x.empty())
		return -1;
	return *(--x.end());
}
int get_min(const multiset<int> &x) {
	if (x.empty())
		return 100000000;
	return *(x.begin());
}

int main (void) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)  {
		scanf("%d", &a[i]);
		v.push_back(a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		v.push_back(b[i]);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
	}
	multiset<int> sa, sb;
	int r = -1;
	for (int i = 0; i < n; i++) {
		if (r < i - 1)
			r = i - 1;
		for (;;) {
			if (r == n - 1)
				break;
			int curr = r + 1;
			if (a[curr] > b[curr])
				break;
			int amax = get_max(sa);
			int bmin = get_min(sb);
			if (a[curr] > bmin || amax > b[curr])
				break;
			r++;
			sa.insert(a[curr]);
			sb.insert(b[curr]);
			occa[a[curr]].push(curr);
			occb[b[curr]].push(curr);
		}
		int amax = get_max(sa);
		int bmin = get_min(sb);
		if (amax == -1 || amax != bmin)
			continue;
		sol += r - max(occa[amax].front(), occb[bmin].front()) + 1;
		occa[a[i]].pop();
		occb[b[i]].pop();
		sa.erase(sa.find(a[i]));
		sb.erase(sb.find(b[i]));
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int n;

int a[maxn], b[maxn];

long long sol;

queue<int> occa[maxn], occb[maxn];

vector<int> v;

int get_max(const multiset<int> &x) {
	if (x.empty())
		return -1;
	return *(--x.end());
}
int get_min(const multiset<int> &x) {
	if (x.empty())
		return 2 * n;
	return *(x.begin());
}

int main (void) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)  {
		scanf("%d", &a[i]);
		v.push_back(a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		v.push_back(b[i]);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
	}
	multiset<int> sa, sb;
	int r = -1;
	for (int i = 0; i < n; i++) {
		if (r < i - 1)
			r = i - 1;
		for (;;) {
			if (r == n - 1)
				break;
			int curr = r + 1;
			if (a[curr] > b[curr])
				break;
			int amax = get_max(sa);
			int bmin = get_min(sb);
			if (a[curr] > bmin || amax > b[curr])
				break;
			r++;
			sa.insert(a[curr]);
			sb.insert(b[curr]);
			occa[a[curr]].push(curr);
			occb[b[curr]].push(curr);
		}
		int amax = get_max(sa);
		int bmin = get_min(sb);
		if (amax == -1 || amax != bmin)
			continue;
		sol += r - max(occa[amax].front(), occb[bmin].front()) + 1;
		occa[a[i]].pop();
		occb[b[i]].pop();
		sa.erase(sa.find(a[i]));
		sb.erase(sb.find(b[i]));
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int n;

int a[maxn], b[maxn];

long long sol;

queue<int> occa[maxn * 2], occb[maxn * 2];

vector<int> v;

int get_max(const multiset<int> &x) {
	if (x.empty())
		return -1;
	return *(--x.end());
}
int get_min(const multiset<int> &x) {
	if (x.empty())
		return 100000000;
	return *(x.begin());
}

int main (void) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)  {
		scanf("%d", &a[i]);
		v.push_back(a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		v.push_back(b[i]);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
	}
	multiset<int> sa, sb;
	int r = -1;
	for (int i = 0; i < n; i++) {
		if (r < i - 1)
			r = i - 1;
		for (;;) {
			if (r == n - 1)
				break;
			int curr = r + 1;
			if (a[curr] > b[curr])
				break;
			int amax = get_max(sa);
			int bmin = get_min(sb);
			if (a[curr] > bmin || amax > b[curr])
				break;
			r++;
			sa.insert(a[curr]);
			sb.insert(b[curr]);
			occa[a[curr]].push(curr);
			occb[b[curr]].push(curr);
		}
		int amax = get_max(sa);
		int bmin = get_min(sb);
		if (amax == -1)
			continue;
		if (amax == bmin)
			sol += r - max(occa[amax].front(), occb[bmin].front()) + 1;
		occa[a[i]].pop();
		occb[b[i]].pop();
		sa.erase(sa.find(a[i]));
		sb.erase(sb.find(b[i]));
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int n;

int a[maxn], b[maxn];

long long sol;

queue<int> occa[maxn], occb[maxn];

vector<int> v;

int get_max(const multiset<int> &x) {
	if (x.empty())
		return -1;
	return *(--x.end());
}
int get_min(const multiset<int> &x) {
	if (x.empty())
		return n;
	return *(x.begin());
}

int main (void) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)  {
		scanf("%d", &a[i]);
		v.push_back(a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		v.push_back(b[i]);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
	}
	multiset<int> sa, sb;
	int r = -1;
	for (int i = 0; i < n; i++) {
		if (r < i - 1)
			r = i - 1;
		for (;;) {
			if (r == n - 1)
				break;
			int curr = r + 1;
			if (a[curr] > b[curr])
				break;
			int amax = get_max(sa);
			int bmin = get_min(sb);
			if (a[curr] > bmin || amax > b[curr])
				break;
			r++;
			sa.insert(a[curr]);
			sb.insert(b[curr]);
			occa[a[curr]].push(curr);
			occb[b[curr]].push(curr);
		}
		int amax = get_max(sa);
		int bmin = get_min(sb);
		if (amax == -1 || amax != bmin)
			continue;
		sol += r - max(occa[amax].front(), occb[bmin].front()) + 1;
		occa[a[i]].pop();
		occb[b[i]].pop();
		sa.erase(sa.find(a[i]));
		sb.erase(sb.find(b[i]));
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define X first
#define Y second
using namespace std;

const int o = 1 << 18;
const int maxn = 200005;
const int inf = 0x3f3f3f3f;

struct tournament {
	int maxi[o * 2], mini[o * 2];
	void init() {
		memset(mini, inf, sizeof mini);
		memset(maxi, -inf, sizeof maxi);
	}
	void update_max(int x, int val) {
		x += o;
		maxi[x] = val;
		for (x /= 2; x; x /= 2)
			maxi[x] = max(maxi[x * 2], maxi[x * 2 + 1]);
	}
	void update_min(int x, int val) {
		x += o;
		mini[x] = val;
		for (x /= 2; x; x /= 2)
			mini[x] = min(mini[x * 2], mini[x * 2 + 1]);
	}
	int query_max(int x, int lo, int hi, int qlo, int qhi) {
		if (lo >= qlo && hi <= qhi)
			return maxi[x];
		if (lo >= qhi || hi <= qlo)
			return -inf;
		int mid = (lo + hi) / 2;
		int l = query_max(x * 2, lo, mid, qlo, qhi);
		int r = query_max(x * 2 + 1, mid, hi, qlo, qhi);
		return max(l, r);
	}
	int query_min(int x, int lo, int hi, int qlo, int qhi) {
		if (lo >= qlo && hi <= qhi)
			return mini[x];
		if (lo >= qhi || hi <= qlo)
			return inf;
		int mid = (lo + hi) / 2;
		int l = query_min(x * 2, lo, mid, qlo, qhi);
		int r = query_min(x * 2 + 1, mid, hi, qlo, qhi);
		return min(l, r);
	}
} ta, tb;

int n;

int a[maxn], b[maxn];

long long sol;

int main (void) {
	scanf("%d", &n);
	ta.init();
	tb.init();
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		ta.update_max(i, a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		tb.update_min(i, b[i]);
	}
	for (int i = 0; i < n; i++) {
		if (a[i] > b[i])
			continue;
		int lo = i;
		int hi = n - 1;
		while (lo < hi) {
			int mid = (lo + hi + 1) / 2;
			if (ta.query_max(1, 0, o, i, mid + 1) > tb.query_min(1, 0, o, i, mid + 1))
				hi = mid - 1;
			else
				lo = mid;
		}
		if (ta.query_max(1, 0, o, i, lo + 1) != tb.query_min(1, 0, o, i, lo + 1))
			continue;
		hi = i;
		int fin = lo;
		while (hi < lo) {
			int mid = (lo + hi) / 2;
			if (ta.query_max(1, 0, o, i, mid + 1) != tb.query_min(1, 0, o, i, mid + 1))
				hi = mid + 1;
			else
				lo = mid;
		}
		sol += fin - lo + 1;
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int n;

int a[maxn], b[maxn];

long long sol;

queue<int> occa[maxn], occb[maxn];

vector<int> v;

int get_max(const multiset<int> &x) {
	if (x.empty())
		return -1;
	return *(--x.end());
}
int get_min(const multiset<int> &x) {
	if (x.empty())
		return 100000000;
	return *(x.begin());
}

int main (void) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)  {
		scanf("%d", &a[i]);
		v.push_back(a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		v.push_back(b[i]);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
	}
	multiset<int> sa, sb;
	int r = -1;
	for (int i = 0; i < n; i++) {
		if (r < i - 1)
			r = i - 1;
		for (;;) {
			if (r == n - 1)
				break;
			int curr = r + 1;
			if (a[curr] > b[curr])
				break;
			int amax = get_max(sa);
			int bmin = get_min(sb);
			if (a[curr] > bmin || amax > b[curr])
				break;
			r++;
			sa.insert(a[curr]);
			sb.insert(b[curr]);
			occa[a[curr]].push(curr);
			occb[b[curr]].push(curr);
		}
		int amax = get_max(sa);
		int bmin = get_min(sb);
		if (amax == -1)
			continue;
		if (amax == bmin)
			sol += r - max(occa[amax].front(), occb[bmin].front()) + 1;
		occa[a[i]].pop();
		occb[b[i]].pop();
		sa.erase(sa.find(a[i]));
		sb.erase(sb.find(b[i]));
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <set>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int n;

int a[maxn], b[maxn]; // a - max

long long sol;

queue<int> occa[maxn], occb[maxn];

vector<int> v;

int get_max(const multiset<int> &x) {
	if (x.empty())
		return -1;
	return *(--x.end());
}
int get_min(const multiset<int> &x) {
	if (x.empty())
		return n;
	return *(x.begin());
}

int main (void) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)  {
		scanf("%d", &a[i]);
		v.push_back(a[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		v.push_back(b[i]);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < v.size(); i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
	}
	multiset<int> sa, sb;
	int r = -1;
	for (int i = 0; i < n; i++) {
		if (r < i - 1)
			r = i - 1;
		for (;;) {
			if (r == n - 1)
				break;
			int curr = r + 1;
			if (a[curr] > b[curr])
				break;
			int amax = get_max(sa);
			int bmin = get_min(sb);
			if (a[curr] > bmin || amax > b[curr])
				break;
			r++;
			sa.insert(a[curr]);
			sb.insert(b[curr]);
			occa[a[curr]].push(curr);
			occb[b[curr]].push(curr);
		}
		int amax = get_max(sa);
		int bmin = get_min(sb);
		if (amax == -1 || amax != bmin)
			continue;
		sol += r - max(occa[amax].front(), occb[bmin].front()) + 1;
		occa[a[i]].pop();
		occb[b[i]].pop();
		sa.erase(sa.find(a[i]));
		sb.erase(sb.find(b[i]));
	}
	printf("%I64d\n", sol);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;

#define bll long long
#define dou double
#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define rep(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define rek(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define Mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int maxn=200000+100,maxD=18;
int N,A[maxn][maxD],B[maxn][maxD],logn[maxn];

void RMQ()
{
    For(j,1,maxD-1)
        For(i,1,N)
        {
            int k=min(N,i+(1<<(j-1)));
            A[i][j]=max(A[i][j-1],A[k][j-1]);
            B[i][j]=max(B[i][j-1],B[k][j-1]);
        }
}

void Prepare()
{
    logn[0]=logn[1]=0;
    For(i,2,maxn-1)
        logn[i]=1+logn[i>>1];
}

int get_RMQ(int A[][maxD],int x,int y)
{
    int j=logn[y-x];
    int k=1<<j;
    return max(A[x][j],A[y-k+1][j]);
}

int find_left(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,mid,k-1)<x) ri=mid;
            else le=mid;
    }
    return ri;
}

int find_right(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,k,mid)<=x) le=mid;
            else ri=mid;
    }
    return le;
}

long long calc(int x,int y,int xx,int yy,int i)
{
    x=max(x,xx);
    y=min(y,yy);
    return (bll)(i-x+1)*(y-i+1);
}

long long Done()
{
    long long ans=0;
    For(i,1,N)
    {
        if (-A[i][0]<B[i][0]) continue;
        int al=find_left(A,0,i,A[i][0],i);
        int ar=find_right(A,i,N+1,A[i][0],i);
        int bl=find_left(B,0,i,-A[i][0],i);
        int br=find_right(B,i,N+1,-A[i][0],i);
        ans+=calc(al,ar,bl,br,i);
//printf("%d %d %d %d %d\n",i,al,ar,bl,br);
        if (-A[i][0]-1>=B[i][0])
        {
            bl=find_left(B,0,i,-A[i][0]-1,i);
            br=find_right(B,i,N+1,-A[i][0]-1,i);
            ans-=calc(al,ar,bl,br,i);
        }
    }
    return ans;
}

int main(int argc, char* argv[])
{
    Prepare();
    for (; scanf("%d",&N)!=EOF; )
    {
        For(i,1,N) scanf("%d",&A[i][0]);
        For(i,1,N) scanf("%d",&B[i][0]);
        For(i,1,N) B[i][0]=-B[i][0];
        RMQ();
        long long ans=Done();
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;

#define bll long long
#define dou double
#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define rep(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define rek(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define Mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int maxn=200000+100,maxD=18;
int N,A[maxn][maxD],B[maxn][maxD],logn[maxn];

void RMQ()
{
    For(j,1,maxD-1)
        For(i,1,N)
        {
            int k=1<<(j-1);
            A[i][j]=max(A[i][j-1],A[i+k][j-1]);
            B[i][j]=max(B[i][j-1],B[i+k][j-1]);
        }
}

void Prepare()
{
    logn[0]=logn[1]=0;
    For(i,2,maxn-1)
        logn[i]=1+logn[i>>1];
}

int get_RMQ(int A[][maxD],int x,int y)
{
    int j=logn[y-x];
    int k=1<<j;
    return max(A[x][j],A[y-k+1][j]);
}

int find_left(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,mid,k)<=x) ri=mid;
            else le=mid;
    }
    return ri;
}

int find_right(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,k,mid)<=x) le=mid;
            else ri=mid;
    }
    return le;
}

long long calc(int x,int y,int xx,int yy,int i)
{
    x=max(x,xx);
    y=min(y,yy);
    return (bll)(i-x+1)*(y-i+1);
}

long long Done()
{
    long long ans=0;
    For(i,1,N)
    {
        if (-A[i][0]<B[i][0]) continue;
        int al=find_left(A,0,i,A[i][0],i);
        int ar=find_right(A,i,N+1,A[i][0],i);
        int bl=find_left(B,0,i,-A[i][0],i);
        int br=find_right(B,i,N+1,-A[i][0],i);
        ans+=calc(al,ar,bl,br,i);
//printf("%d %d %d %d %d\n",i,al,ar,bl,br);
        if (-A[i][0]-1>=B[i][0])
        {
            bl=find_left(B,0,i,-A[i][0]-1,i);
            br=find_right(B,i,N+1,-A[i][0]-1,i);
            ans-=calc(al,ar,bl,br,i);
        }
    }
    return ans;
}

int main(int argc, char* argv[])
{
    Prepare();
    for (; scanf("%d",&N)!=EOF; )
    {
        For(i,1,N) scanf("%d",&A[i][0]);
        For(i,1,N) scanf("%d",&B[i][0]);
        For(i,1,N) B[i][0]=-B[i][0];
        RMQ();
        long long ans=Done();
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;

#define bll long long
#define dou double
#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define rep(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define rek(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define Mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int maxn=200000+100,maxD=18;
int N,A[maxn][maxD],B[maxn][maxD],logn[maxn];

void RMQ()
{
    For(j,1,maxD-1)
        For(i,1,N)
        {
            int k=min(N,i+(1<<(j-1)));
            A[i][j]=max(A[i][j-1],A[k][j-1]);
            B[i][j]=max(B[i][j-1],B[k][j-1]);
        }
}

void Prepare()
{
    logn[0]=logn[1]=0;
    For(i,2,maxn-1)
        logn[i]=1+logn[i>>1];
}

int get_RMQ(int A[][maxD],int x,int y)
{
    int j=logn[y-x];
    int k=1<<j;
    return max(A[x][j],A[y-k+1][j]);
}

int find_left_A(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,mid,k-1)<x) ri=mid;
            else le=mid;
    }
    return ri;
}

int find_left(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,mid,k)<=x) ri=mid;
            else le=mid;
    }
    return ri;
}

int find_right(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,k,mid)<=x) le=mid;
            else ri=mid;
    }
    return le;
}

long long calc(int x,int y,int xx,int yy,int i)
{
    x=max(x,xx);
    y=min(y,yy);
    return (bll)(i-x+1)*(y-i+1);
}

long long Done()
{
    long long ans=0;
    For(i,1,N)
    {
        if (-A[i][0]<B[i][0]) continue;
        int al=find_left_A(A,0,i,A[i][0],i);
        int ar=find_right(A,i,N+1,A[i][0],i);
        int bl=find_left(B,0,i,-A[i][0],i);
        int br=find_right(B,i,N+1,-A[i][0],i);
        ans+=calc(al,ar,bl,br,i);
//printf("%d %d %d\n",i,max(al,bl),min(ar,br));
        if (-A[i][0]-1>=B[i][0])
        {
            bl=find_left(B,0,i,-A[i][0]-1,i);
            br=find_right(B,i,N+1,-A[i][0]-1,i);
//printf("--  %d %d %d\n",i,max(al,bl),min(ar,br));
            ans-=calc(al,ar,bl,br,i);
        }
//        printf("! %lld\n",ans);
    }
    return ans;
}

int main(int argc, char* argv[])
{
    Prepare();
    for (; scanf("%d",&N)!=EOF; )
    {
        For(i,1,N) scanf("%d",&A[i][0]);
        For(i,1,N) scanf("%d",&B[i][0]);
        For(i,1,N) B[i][0]=-B[i][0];
        RMQ();
        long long ans=Done();
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;

#define bll long long
#define dou double
#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define rep(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define rek(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define Mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int maxn=200000+100,maxD=18;
int N,A[maxn][maxD],B[maxn][maxD],logn[maxn];

void RMQ()
{
    For(j,1,maxD-1)
        For(i,1,N)
        {
            int k=min(N,i+(1<<(j-1)));
            A[i][j]=max(A[i][j-1],A[k][j-1]);
            B[i][j]=max(B[i][j-1],B[k][j-1]);
        }
}

void Prepare()
{
    logn[0]=logn[1]=0;
    For(i,2,maxn-1)
        logn[i]=1+logn[i>>1];
}

int get_RMQ(int A[][maxD],int x,int y)
{
    int j=logn[y-x];
    int k=1<<j;
    return max(A[x][j],A[y-k+1][j]);
}

int find_left(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,mid,k)<=x) ri=mid;
            else le=mid;
    }
    return ri;
}

int find_right(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,k,mid)<=x) le=mid;
            else ri=mid;
    }
    return le;
}

long long calc(int x,int y,int xx,int yy,int i)
{
    x=max(x,xx);
    y=min(y,yy);
    return (bll)(i-x+1)*(y-i+1);
}

long long Done()
{
    long long ans=0;
    For(i,1,N)
    {
        if (-A[i][0]<B[i][0]) continue;
        int al=find_left(A,0,i,A[i][0],i);
        int ar=find_right(A,i,N+1,A[i][0],i);
        int bl=find_left(B,0,i,-A[i][0],i);
        int br=find_right(B,i,N+1,-A[i][0],i);
        ans+=calc(al,ar,bl,br,i);
//printf("%d %d %d %d %d\n",i,al,ar,bl,br);
        if (-A[i][0]-1>=B[i][0])
        {
            bl=find_left(B,0,i,-A[i][0]-1,i);
            br=find_right(B,i,N+1,-A[i][0]-1,i);
            ans-=calc(al,ar,bl,br,i);
        }
    }
    return ans;
}

int main(int argc, char* argv[])
{
    Prepare();
    for (; scanf("%d",&N)!=EOF; )
    {
        For(i,1,N) scanf("%d",&A[i][0]);
        For(i,1,N) scanf("%d",&B[i][0]);
        For(i,1,N) B[i][0]=-B[i][0];
        RMQ();
        long long ans=Done();
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;

#define bll long long
#define dou double
#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define rep(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define rek(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define Mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int maxn=200000+100,maxD=18;
int N,A[maxn][maxD],B[maxn][maxD],logn[maxn];

void RMQ()
{
    For(j,1,maxD-1)
        For(i,1,N)
        {
            int k=min(N,i+(1<<(j-1)));
            A[i][j]=max(A[i][j-1],A[k][j-1]);
            B[i][j]=max(B[i][j-1],B[k][j-1]);
        }
}

void Prepare()
{
    logn[0]=logn[1]=0;
    For(i,2,maxn-1)
        logn[i]=1+logn[i>>1];
}

int get_RMQ(int A[][maxD],int x,int y)
{
    int j=logn[y-x];
    int k=1<<j;
    return max(A[x][j],A[y-k+1][j]);
}

int find_left(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,mid,k)<x) ri=mid;
            else le=mid;
    }
    return ri;
}

int find_right(int A[][maxD],int le,int ri,int x,int k)
{
    while(le+1<ri)
    {
        int mid=(le+ri)>>1;
        if (get_RMQ(A,k,mid)<=x) le=mid;
            else ri=mid;
    }
    return le;
}

long long calc(int x,int y,int xx,int yy,int i)
{
    x=max(x,xx);
    y=min(y,yy);
    return (bll)(i-x+1)*(y-i+1);
}

long long Done()
{
    long long ans=0;
    For(i,1,N)
    {
        if (-A[i][0]<B[i][0]) continue;
        int al=find_left(A,0,i,A[i][0],i);
        int ar=find_right(A,i,N+1,A[i][0],i);
        int bl=find_left(B,0,i,-A[i][0],i);
        int br=find_right(B,i,N+1,-A[i][0],i);
        ans+=calc(al,ar,bl,br,i);
//printf("%d %d %d %d %d\n",i,al,ar,bl,br);
        if (-A[i][0]-1>=B[i][0])
        {
            bl=find_left(B,0,i,-A[i][0]-1,i);
            br=find_right(B,i,N+1,-A[i][0]-1,i);
            ans-=calc(al,ar,bl,br,i);
        }
    }
    return ans;
}

int main(int argc, char* argv[])
{
    Prepare();
    for (; scanf("%d",&N)!=EOF; )
    {
        For(i,1,N) scanf("%d",&A[i][0]);
        For(i,1,N) scanf("%d",&B[i][0]);
        For(i,1,N) B[i][0]=-B[i][0];
        RMQ();
        long long ans=Done();
        printf("%I64d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[200010], b[200010];
int Ta[200010], Tb[200010];
ll ans;
int n, Ma, Mi;
int lowbit(int &x)
{
	return x & (-x);
}
void upA(int &x, int &c)
{
	for(int i = x; i <= n; i += lowbit(i))
		Ta[i] = max(Ta[i], c);
}
void upB(int &x, int &c)
{
	for(int i = x; i <= n; i += lowbit(x))
		Tb[i] = min(Tb[i], c);
}
int Max(int &x)
{
	int res = -(1e9 + 1);
	for(int i = x; i > 0; i -= lowbit(i))
		res = max(Ta[i], res);
	return res;
}
int Min(int &x)
{
	int res = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i))
		res = min(Tb[i], res);
	return res;
}
void Get(int &x)
{
	Ma = -(1e9 + 1);
	Mi = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i)){
		Ma = max(Ta[i], Ma);
		Mi = min(Tb[i], Mi);
	}
}
void up(int &x, int &c1, int &c2)
{
	for(int i = x; i <= n; i += lowbit(i)){
		Ta[i] = max(Ta[i], c1);
		Tb[i] = min(Tb[i], c2);
	}
}
int main()
{
//	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", a + i);
		Ta[i] = -(1e9 + 1);
	}
	for(int i = 1; i <= n; i++){
		scanf("%d", b + i);
		Tb[i] = 1e9 + 1;
	}
	for(int i = n; i > 0; i--){
		int rM;
		upA(i, a[i]);
		upB(i, b[i]);
	//	up(i, a[i], b[i]);
		if(Max(i) > Min(i))
			continue;
		int l = i, r = n;
		int rm = 0;
		while(l <= r){
			int mid = (l + r) >> 1;
			Get(mid);
			int t = Ma - Mi;
			if(t >= 0){
				if(t == 0)
					rm = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if(!rm)
			continue;
		l = rm; r = n;
		while(l <= r){
			int mid = (l + r) >> 1;
			Get(mid);
			int t = Ma - Mi;
			if(t > 0)
				r = mid - 1;
			else{
				rM = mid;
				l = mid + 1;
			}
		}
		ans += rM - rm + 1;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[200010], b[200010];
int Ta[200010], Tb[200010];
ll ans;
int n, Ma, Mi;
int lowbit(int &x)
{
	return x & (-x);
}
void upA(int &x, int c)
{
	for(int i = x; i <= n; i += lowbit(i))
		Ta[i] = max(Ta[i], c);
}
void upB(int &x, int c)
{
	for(int i = x; i <= n; i += lowbit(x))
		Tb[i] = min(Tb[i], c);
}
int Max(int &x)
{
	int res = -(1e9 + 1);
	for(int i = x; i > 0; i -= lowbit(i))
		res = max(Ta[i], res);
	return res;
}
int Min(int &x)
{
	int res = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i))
		res = min(Tb[i], res);
	return res;
}
void Get(int &x)
{
	Ma = -(1e9 + 1);
	Mi = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i)){
		Ma = max(Ta[i], Ma);
		Mi = min(Tb[i], Mi);
	}
}
void up(int &x, int &c1, int &c2)
{
	for(int i = x; i <= n; i += lowbit(i)){
		Ta[i] = max(Ta[i], c1);
		Tb[i] = min(Tb[i], c2);
	}
}
int main()
{
//	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", a + i);
		Ta[i] = -(1e9 + 1);
	}
	for(int i = 1; i <= n; i++){
		scanf("%d", b + i);
		Tb[i] = 1e9 + 1;
	}
	for(int i = n; i > 0; i--){
		int rM;
	//	upA(i, a[i]);
	//	upB(i, b[i]);
		up(i, a[i], b[i]);
		if(Max(i) > Min(i))
			continue;
		int l = i, r = n;
		int rm = 0;
		while(l <= r){
			int mid = (l + r) >> 1;
			Get(mid);
			int t = Ma - Mi;
			if(t >= 0){
				if(t == 0)
					rm = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if(!rm)
			continue;
		l = rm; r = n;
		while(l <= r){
			int mid = (l + r) >> 1;
			Get(mid);
			int t = Ma - Mi;
			if(t > 0)
				r = mid - 1;
			else{
				rM = mid;
				l = mid + 1;
			}
		}
		ans += rM - rm + 1;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[200010], b[200010];
int Ta[200010], Tb[200010];
ll ans;
int n;
int lowbit(int &x)
{
	return x & (-x);
}
void upA(int &x, int c)
{
	Ta[x] = c;
	for(int i = x; i <= n; i += lowbit(i))
		Ta[i] = max(Ta[i], c);
}
void upB(int &x, int c)
{
	Tb[x] = c;
	for(int i = x; i <= n; i += lowbit(x))
		Tb[i] = min(Tb[i], c);
}
int Max(int &x)
{
	int res = -(1e9 + 1);
	for(int i = x; i > 0; i -= lowbit(i))
		res = max(Ta[i], res);
	return res;
}
int Min(int &x)
{
	int res = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i))
		res = min(Tb[i], res);
	return res;
}
int main()
{
//	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", a + i);
		Ta[i] = -(1e9 + 1);
	}
	for(int i = 1; i <= n; i++){
		scanf("%d", b + i);
		Tb[i] = 1e9 + 1;
	}
	for(int i = n; i > 0; i--){
		int rM;
		upA(i, a[i]);
		upB(i, b[i]);
		if(Max(i) > Min(i))
			continue;
		int l = i, r = n;
		int rm = 0;
		while(l <= r){
			int mid = (l + r) >> 1;
			int t = Max(mid) - Min(mid);
			if(t >= 0){
				if(t == 0)
					rm = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if(!rm)
			continue;
		l = rm; r = n;
		while(l <= r){
			int mid = (l + r) >> 1;
			int t = Max(mid) - Min(mid);
			if(t > 0)
				r = mid - 1;
			else{
				rM = mid;
				l = mid + 1;
			}
		}
		ans += rM - rm + 1;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[200010], b[200010];
int Ta[200010], Tb[200010];
ll ans;
int n, Ma, Mi;
int lowbit(int &x)
{
	return x & (-x);
}
void Get(int &x)
{
	Ma = -(1e9 + 1);
	Mi = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i)){
		Ma = max(Ta[i], Ma);
		Mi = min(Tb[i], Mi);
	}
}
void up(int &x, int &c1, int &c2)
{
	for(int i = x; i <= n; i += lowbit(i)){
		Ta[i] = max(Ta[i], c1);
		Tb[i] = min(Tb[i], c2);
	}
}
int main()
{
//	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", a + i);
		Ta[i] = -(1e9 + 1);
	}
	for(int i = 1; i <= n; i++){
		scanf("%d", b + i);
		Tb[i] = 1e9 + 1;
	}
	for(int i = n; i > 0; i--){
		int rM;
		up(i, a[i], b[i]);
		Get(i);
		if(Ma > Mi)
			continue;
		int l = i, r = n;
		int rm = 0;
		while(l <= r){
			int mid = (l + r) >> 1;
			Get(mid);
			int t = Ma - Mi;
			if(t >= 0){
				if(t == 0)
					rm = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if(!rm)
			continue;
		l = rm; r = n;
		while(l <= r){
			int mid = (l + r) >> 1;
			Get(mid);
			int t = Ma - Mi;
			if(t > 0)
				r = mid - 1;
			else{
				rM = mid;
				l = mid + 1;
			}
		}
		ans += rM - rm + 1;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[200010], b[200010];
int Ta[200010], Tb[200010];
ll ans;
int n, Ma, Mi;
int lowbit(int &x)
{
	return x & (-x);
}
void upA(int &x, int &c)
{
	for(int i = x; i <= n; i += lowbit(i))
		Ta[i] = max(Ta[i], c);
}
void upB(int &x, int &c)
{
	for(int i = x; i <= n; i += lowbit(x))
		Tb[i] = min(Tb[i], c);
}
int Max(int &x)
{
	int res = -(1e9 + 1);
	for(int i = x; i > 0; i -= lowbit(i))
		res = max(Ta[i], res);
	return res;
}
int Min(int &x)
{
	int res = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i))
		res = min(Tb[i], res);
	return res;
}
void Get(int &x)
{
	Ma = -(1e9 + 1);
	Mi = 1e9 + 1;
	for(int i = x; i > 0; i -= lowbit(i)){
		Ma = max(Ta[i], Ma);
		Mi = min(Tb[i], Mi);
	}
}
void up(int &x, int &c1, int &c2)
{
	for(int i = x; i <= n; i += lowbit(i)){
		Ta[i] = max(Ta[i], c1);
		Tb[i] = min(Tb[i], c2);
	}
}
int main()
{
//	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", a + i);
		Ta[i] = -(1e9 + 1);
	}
	for(int i = 1; i <= n; i++){
		scanf("%d", b + i);
		Tb[i] = 1e9 + 1;
	}
	for(int i = n; i > 0; i--){
		int rM;
	//	upA(i, a[i]);
	//	upB(i, b[i]);
		up(i, a[i], b[i]);
		if(Max(i) > Min(i))
			continue;
		int l = i, r = n;
		int rm = 0;
		while(l <= r){
			int mid = (l + r) >> 1;
		//	Get(mid);
			int t = Max(mid) - Min(mid);
			if(t >= 0){
				if(t == 0)
					rm = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if(!rm)
			continue;
		l = rm; r = n;
		while(l <= r){
			int mid = (l + r) >> 1;
		//	Get(mid);
			int t = Max(mid) - Min(mid);
			if(t > 0)
				r = mid - 1;
			else{
				rM = mid;
				l = mid + 1;
			}
		}
		ans += rM - rm + 1;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[100005];
int b[100005];

int s1[100005];
int i1[100005];
int i2[100005];
int s2[100005];
int sz1, sz2;
int ans[100005];

bool cmp1(const int &x, const int &y)
{
    return x < y;
}

bool cmp2(const int &x, const int &y)
{
    return x > y;
}

int inter(int x1, int x2, int y1, int y2)
{
    y1 = max(y1, x1);
    y2 = min(y2, x2);
    if (y1 <= y2)
        return y2 - y1 + 1;
    return 0;
}

int main()
{
    memset(ans, 0, sizeof(ans));

    long long res = 0;
    sz2 = sz2 = 0;
    int 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);

    for (int i = 0; i < n; i++)
    {
        while (sz1 > 0 && s1[sz1-1] <= a[i])
            sz1--;
        while (sz2 > 0 && s2[sz2-1] >= b[i])
            sz2--;
        s1[sz1] = a[i];
        i1[sz1] = i;
        s2[sz2] = b[i];
        i2[sz2] = i;
        sz1++;
        sz2++;
        if (sz1 == 1)
        {
            int ind = lower_bound(s2, s2 + sz2, a[i], cmp1) - s2;
            if (ind == sz2 || s2[ind] != a[i])
                continue;
            int st = 0;
            if (ind > 0)
                st = i2[ind - 1];
            ans[i] = inter(0, i, st, i2[ind]);
        }
        else if (sz2 == 1)
        {
            int ind = lower_bound(s1, s1 + sz1, a[i], cmp2) - s1;
            if (ind == sz1 || s1[ind] != b[i])
                continue;
            int st = 0;
            if (ind > 0)
                st = i1[ind - 1];
            ans[i] = inter(0, i, st, i1[ind]);
        }
        else
        {
            int len1 = i - i1[sz1-2];
            int len2 = i - i2[sz2-2];
            if (len1 > len2)
            {
                ans[i] += ans[i1[sz1-2]];
                int ind = lower_bound(s2, s2 + sz2, a[i], cmp1) - s2;
                if (ind == sz2 || s2[ind] != a[i])
                    continue;
                int st = 0;
                if (ind > 0)
                    st = i2[ind - 1];
                ans[i] += inter(i1[sz1-2], i, st, i2[ind]);
            }
            else
            {
                int ind = lower_bound(s1, s1 + sz1, a[i], cmp2) - s1;
                if (ind == sz1 || s1[ind] != b[i])
                    continue;
                int st = 0;
                if (ind > 0)
                    st = i1[ind - 1];
                ans[i] += inter(i2[sz2-2], i, st, i1[ind]);
            }
        }
    }
    for (int i = 0; i < n; i++)
        res += ans[i];
    printf("%lld\n", res);
}


#include<bits/stdc++.h>
using namespace std;

const int MAXN = 200005;
int a[MAXN];
int b[MAXN];

int s1[MAXN];
int i1[MAXN];
int i2[MAXN];
int s2[MAXN];
int sz1, sz2;
int ans[MAXN];

int inter(int x1, int x2, int y1, int y2)
{
    y1 = max(y1, x1);
    y2 = min(y2, x2);
    if (y1 <= y2)
        return y2 - y1 + 1;
    return 0;
}

int main()
{
    long long res = 0;
    sz2 = sz2 = 0;
    int 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);

    for (int i = 0; i < n; i++)
    {
        while (sz1 > 0 && s1[sz1-1] <= a[i])
            sz1--;
        while (sz2 > 0 && s2[sz2-1] >= b[i])
            sz2--;
        s1[sz1] = a[i];
        i1[sz1] = i;
        s2[sz2] = b[i];
        i2[sz2] = i;
        sz1++;
        sz2++;
        int lo = 0;
        int hi = i;
        int md = 0;
        int found = -1;
        while (lo <= hi)
        {
            md = (lo + hi) / 2;
            int ind1 = lower_bound(i1, i1 + sz1, md) - i1;
            int ind2 = lower_bound(i2, i2 + sz2, md) - i2;
            if (s1[ind1] > s2[ind2])
                lo = md + 1;
            else if (s1[ind1] < s2[ind2])
                hi = md - 1;
            else
            {
                found = md;
                break;
            }
        }
        if (found == -1)
            continue;
        int ind1 = lower_bound(i1, i1 + sz1, md) - i1;
        int ind2 = lower_bound(i2, i2 + sz2, md) - i2;

        int x2 = i1[ind1];
        int y2 = i2[ind2];
        int x1, y1;
        x1 = y1 = 0;
        if (ind1 > 0)
            x1 = i1[ind1-1] + 1;
        if (ind2 > 0)
            y1 = i2[ind2-1] + 1;
        res += inter(x1, x2, y1, y2);
    }
    printf("%lld\n", res);
}


#include<bits/stdc++.h>
using namespace std;

int a[100005];
int b[100005];

int s1[100005];
int i1[100005];
int i2[100005];
int s2[100005];
int sz1, sz2;
int ans[100005];

bool cmp1(const int &x, const int &y)
{
    return x < y;
}

bool cmp2(const int &x, const int &y)
{
    return x > y;
}

int inter(int x1, int x2, int y1, int y2)
{
    y1 = max(y1, x1);
    y2 = min(y2, x2);
    if (y1 <= y2)
        return y2 - y1 + 1;
    return 0;
}

int main()
{
    memset(ans, 0, sizeof(ans));

    long long res = 0;
    sz2 = sz2 = 0;
    int 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);

    for (int i = 0; i < n; i++)
    {
        while (sz1 > 0 && s1[sz1-1] <= a[i])
            sz1--;
        while (sz2 > 0 && s2[sz2-1] >= b[i])
            sz2--;
        s1[sz1] = a[i];
        i1[sz1] = i;
        s2[sz2] = b[i];
        i2[sz2] = i;
        sz1++;
        sz2++;
        if (sz1 == 1)
        {
            int ind = lower_bound(s2, s2 + sz2, a[i], cmp1) - s2;
            if (ind == sz2 || s2[ind] != a[i])
                continue;
            int st = 0;
            if (ind > 0)
                st = i2[ind - 1]+1;
            ans[i] = inter(0, i, st, i2[ind]);
        }
        else if (sz2 == 1)
        {
            int ind = lower_bound(s1, s1 + sz1, a[i], cmp2) - s1;
            if (ind == sz1 || s1[ind] != b[i])
                continue;
            int st = 0;
            if (ind > 0)
                st = i1[ind - 1] + 1;
            ans[i] = inter(0, i, st, i1[ind]);
        }
        else
        {
            int len1 = i - i1[sz1-2];
            int len2 = i - i2[sz2-2];
            if (len1 > len2)
            {
                ans[i] += ans[i1[sz1-2]];
                int ind = lower_bound(s2, s2 + sz2, a[i], cmp1) - s2;
                if (ind == sz2 || s2[ind] != a[i])
                    continue;
                int st = 0;
                if (ind > 0)
                    st = i2[ind - 1] + 1;
                ans[i] += inter(i1[sz1-2] + 1, i, st, i2[ind]);
            }
            else
            {
                ans[i] += ans[i2[sz2-2]];
                int ind = lower_bound(s1, s1 + sz1, a[i], cmp2) - s1;
                if (ind == sz1 || s1[ind] != b[i])
                    continue;
                int st = 0;
                if (ind > 0)
                    st = i1[ind - 1] + 1;
                ans[i] += inter(i2[sz2-2] + 1, i, st, i1[ind]);
            }
        }
    }
    for (int i = 0; i < n; i++)
        res += ans[i];
    printf("%lld\n", res);
}


#include<bits/stdc++.h>
using namespace std;

int a[100005];
int b[100005];

int s1[100005];
int i1[100005];
int i2[100005];
int s2[100005];
int sz1, sz2;
int ans[100005];

bool cmp1(const int &x, const int &y)
{
    return x < y;
}

bool cmp2(const int &x, const int &y)
{
    return x > y;
}

int inter(int x1, int x2, int y1, int y2)
{
    y1 = max(y1, x1);
    y2 = min(y2, x2);
    if (y1 <= y2)
        return y2 - y1 + 1;
    return 0;
}

int main()
{
    memset(ans, 0, sizeof(ans));

    long long res = 0;
    sz2 = sz2 = 0;
    int 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);

    for (int i = 0; i < n; i++)
    {
        while (sz1 > 0 && s1[sz1-1] <= a[i])
            sz1--;
        while (sz2 > 0 && s2[sz2-1] >= b[i])
            sz2--;
        s1[sz1] = a[i];
        i1[sz1] = i;
        s2[sz2] = b[i];
        i2[sz2] = i;
        sz1++;
        sz2++;
        if (sz1 == 1)
        {
            int ind = lower_bound(s2, s2 + sz2, a[i], cmp1) - s2;
            if (ind == sz2 || s2[ind] != a[i])
                continue;
            int st = 0;
            if (ind > 0)
                st = i2[ind - 1]+1;
            ans[i] = inter(0, i, st, i2[ind]);
        }
        else if (sz2 == 1)
        {
            int ind = lower_bound(s1, s1 + sz1, b[i], cmp2) - s1;
            if (ind == sz1 || s1[ind] != b[i])
                continue;
            int st = 0;
            if (ind > 0)
                st = i1[ind - 1] + 1;
            ans[i] = inter(0, i, st, i1[ind]);
        }
        else
        {
            int len1 = i - i1[sz1-2];
            int len2 = i - i2[sz2-2];
            if (len1 > len2)
            {
                ans[i] += ans[i1[sz1-2]];
                int ind = lower_bound(s2, s2 + sz2, a[i], cmp1) - s2;
                if (ind == sz2 || s2[ind] != a[i])
                    continue;
                int st = 0;
                if (ind > 0)
                    st = i2[ind - 1] + 1;
                ans[i] += inter(i1[sz1-2] + 1, i, st, i2[ind]);
            }
            else
            {
                ans[i] += ans[i2[sz2-2]];
                int ind = lower_bound(s1, s1 + sz1, b[i], cmp2) - s1;
                if (ind == sz1 || s1[ind] != b[i])
                    continue;
                int st = 0;
                if (ind > 0)
                    st = i1[ind - 1] + 1;
                ans[i] += inter(i2[sz2-2] + 1, i, st, i1[ind]);
            }
        }
    }
    for (int i = 0; i < n; i++)
        res += ans[i];
    printf("%lld\n", res);
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;
int n , a[200000],b[200000],st[2][22][200001],low,high,ll,rr,mx,mn,lg[200021];
long long ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        lg[i]=log2(i);
        if(1<<lg[i] > i){
            lg[i]--;
        }
    }
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&b[i]);
    }
    for(int i=0;i<n;i++){
        st[0][0][i]=a[i];
        st[1][0][i]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=0;i<n;i++){
            st[0][j][i]=st[0][j-1][i];
            st[1][j][i]=st[1][j-1][i];
            if(i+(1<<(j-1))<n){
                st[0][j][i]=max(st[0][j][i],st[0][j-1][i+(1<<(j-1))]);
                st[1][j][i]=min(st[1][j][i],st[1][j-1][i+(1<<(j-1))]);
            }

        }
    }
 //   cout<<st[0][lg[2]][0]<<endl;
    bool br=0;
    int idx[2];
    for(int i=0;i<n;i++){
        idx[0]=idx[1]=-1;
        ll=i,rr=n-1;
        br=0;
        while(!br&&ll<=rr){
            mx=-1e9 - 9,mn=1e9+9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=(mid-i)+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-lg[len]-1]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-lg[len]-1]);
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                 continue;
            }
            if(mx==mn){
                idx[0]=mid;
                rr=mid-1;
            }
        }
        br=0;
        ll=i,rr=n-1;
        while(!br&&ll<=rr){
            mx=-1e9 - 9,mn=1e9+9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=mid-i+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-lg[len]-1]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-lg[len]-1]);
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                continue;

            }
            if(mx==mn){
                idx[1]=mid;
                ll=mid+1;
                continue;

            }
        }
       // cout<<idx[0]<<" "<<idx[1]<<endl;
        if(idx[0]==-1){continue;}
        ans+=idx[1]-idx[0]+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;
int n , a[200000],b[200000],st[2][18][200000],low,high,ll,rr,mx,mn,lg[200001];
long long ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        lg[i]=log2(i);
        if(1<<lg[i] > i){
            lg[i]--;
        }
    }
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&b[i]);
    }
    for(int i=0;i<n;i++){
        st[0][0][i]=a[i];
        st[1][0][i]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=0;i<n;i++){
            st[0][j][i]=st[0][j-1][i];
            st[1][j][i]=st[1][j-1][i];
            if(i+(1<<(j-1))<n){
                st[0][j][i]=max(st[0][j][i],st[0][j-1][i+(1<<(j-1))]);
                st[1][j][i]=min(st[1][j][i],st[1][j-1][i+(1<<(j-1))]);
            }

        }
    }
 //   cout<<st[0][lg[2]][0]<<endl;
    bool br=0;
    int idx[2];
    for(int i=0;i<n;i++){
        idx[0]=idx[1]=-1;
        ll=i,rr=n-1;
        br=0;
        while(!br&&ll<=rr){
            mx=-1,mn=1e9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=(mid-i)+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-lg[len]-1]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-lg[len]-1]);
           // cout<<mid<<" "<<i<<" "<<mx<<" "<<mn<<" "<<i+len-lg[len]-1<<endl;
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                 continue;
            }
            if(mx==mn){
                idx[0]=mid;
                rr=mid-1;
            }
        }
       // cout<<mx<<" "<<mn<<endl;
        br=0;
        ll=i,rr=n-1;
        while(!br&&ll<=rr){
            mx=-1,mn=1e9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=mid-i+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-lg[len]-1]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-lg[len]-1]);
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                continue;

            }
            if(mx==mn){
                idx[1]=mid;
                ll=mid+1;
                continue;

            }
        }
       // cout<<idx[0]<<" "<<idx[1]<<endl;
        if(idx[0]==-1){continue;}
        ans+=idx[1]-idx[0]+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;
class SegmentTree{
public:
    int ans,a[200000],st[800000],i,j;

    void pull(int idx){
        st[idx]=min(st[idx<<1],st[(idx<<1)+1]);
    }
    void build(int idx,int l,int r){
        if(l==r){
            st[idx]=a[l];
            return ;
        }
        build(idx<<1,l,(l+r)>>1);
        build((idx<<1)+1,(l+r)/2 +1,r);
        pull(idx);
    }
    void get(int idx,int l,int r){
        if(l>j||r<i){
            return ;
        }
        if(l>=i&&r<=j){
            ans=min(ans,st[idx]);
            return ;
        }
        get(idx<<1,l,(l+r)>>1);
        get((idx<<1)+1,(l+r)/2 +1,r);
    }
};
int n ;
SegmentTree S[2];
inline pair<int,int> cal(int x ,int y){
    int ret[2];
    for(int i=0;i<2;i++){
        S[i].ans=1e9;S[i].ans+=99;
        S[i].i=x;S[i].j=y;
        S[i].get(1,0,n-1);
        ret[i]=S[i].ans;
    }
    return make_pair(ret[0],ret[1]);
}
int bs_last(int x){
    int l=0,r=(n-1)-x,ret=-1;
    pair<int,int> c;
    bool br=0;
    while(l<=r&&!br){
        int mid=l+(r-l)/2;
        if(l==r){
            br=1;
            c=cal(x,x+l);
        }else{
            c=cal(x,x+mid);
        }
        if(-c.first>c.second){
            r=mid-1;
            continue;
        }
        if(-c.first<c.second){
            l=mid+1;
            continue;
        }
        if(-c.first==c.second){
            l=mid+1;
            ret=max(ret,mid);
            continue;
        }
    }
    return ret;
}
int bs_first(int x){
    int l=0,r=(n-1)-x,ret=1e9;
    pair<int,int> c;
    bool br=0;
    while(l<=r&&!br){
        int mid=l+(r-l)/2;
        if(l==r){
            br=1;
            c=cal(x,x+l);
        }else{
            c=cal(x,x+mid);
        }
        if(-c.first>c.second){
            r=mid-1;
            continue;
        }
        if(-c.first<c.second){
            l=mid+1;
            continue;
        }
        if(-c.first==c.second){
            r=mid-1;
            ret=min(ret,mid);
            continue;
        }
    }
    return ret;
}

int main(){
    scanf("%d",&n);
    int si=-1;
    for(int j=0;j<2;j++){
        for(int i=0;i<n;i++){
            scanf("%d",&S[j].a[i]);
            S[j].a[i]*=si;
        }
        si=1;
        S[j].build(1,0,n-1);
    }
    long long ans=0;
    pair<int,int> range;
    for(int i=0;i<n;i++){
        range.first=bs_first(i);
        range.second=bs_last(i);
        if(range.first==1e9||range.second==-1){
            continue;
        }
        ans+=range.second-range.first+1;
    }

    printf("%lld\n",ans);

    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;
int n , a[200000],b[200000],st[2][18][200000],low,high,ll,rr,mx,mn,lg[200001];
long long ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        lg[i]=log2(i);
        if(1<<lg[i] > i){
            lg[i]--;
        }
    }
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&b[i]);
    }
    for(int i=0;i<n;i++){
        st[0][0][i]=a[i];
        st[1][0][i]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=0;i<n;i++){
            st[0][j][i]=st[0][j-1][i];
            st[1][j][i]=st[1][j-1][i];
            if(i+(1<<(j-1))<n){
                st[0][j][i]=max(st[0][j][i],st[0][j-1][i+(1<<(j-1))]);
                st[1][j][i]=min(st[1][j][i],st[1][j-1][i+(1<<(j-1))]);
            }

        }
    }
    //   cout<<st[0][lg[2]][0]<<endl;
    bool br=0;
    int idx[2];
    for(int i=0;i<n;i++){
        idx[0]=idx[1]=-1;
        ll=i,rr=n-1;
        br=0;
        while(!br&&ll<=rr){
            mx=-1,mn=1e9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=(mid-i)+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-(1<<lg[len])]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-(1<<lg[len])]);
            // cout<<mid<<" "<<i<<" "<<mx<<" "<<mn<<" "<<i+len-lg[len]-1<<endl;
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                continue;
            }
            if(mx==mn){
                idx[0]=mid;
                rr=mid-1;
            }
        }
        // cout<<mx<<" "<<mn<<endl;
        br=0;
        ll=i,rr=n-1;
        while(!br&&ll<=rr){
            mx=-1,mn=1e9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=mid-i+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-(1<<lg[len])]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-(1<<lg[len])]);
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                continue;

            }
            if(mx==mn){
                idx[1]=mid;
                ll=mid+1;
                continue;

            }
        }
        // cout<<idx[0]<<" "<<idx[1]<<endl;
        if(idx[0]==-1){continue;}
        ans+=idx[1]-idx[0]+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;
const int oo=1000000009;
int dx[8]={1,-1,0,0,1,-1,1,-1};
int dy[8]={0,0,-1,1,1,-1,-1,1};
class SegmentTree{
public:
    int ans,a[200022],st[800088],i,j;

    void pull(int idx){
        st[idx]=min(st[idx<<1],st[(idx<<1)+1]);
    }
    void build(int idx,int l,int r){
        if(l==r){
            st[idx]=a[l];
            return ;
        }
        build(idx<<1,l,(l+r)>>1);
        build((idx<<1)+1,(l+r)/2 +1,r);
        pull(idx);
    }
    void get(int idx,int l,int r){
        if(l>j||r<i){
            return ;
        }
        if(l>=i&&r<=j){
            ans=min(ans,st[idx]);
            return ;
        }
        get(idx<<1,l,(l+r)>>1);
        get((idx<<1)+1,(l+r)/2 +1,r);
    }
};
int n ;
SegmentTree S[2];

inline pair<int,int> cal(int x ,int y){
    int ret[2];
    for(int i=0;i<2;i++){
        S[i].ans=1e9;S[i].ans+=99;
        S[i].i=x;S[i].j=y;
        S[i].get(1,0,n-1);
        ret[i]=S[i].ans;
    }
    return make_pair(ret[0],ret[1]);
}
int bs_last(int x){
    int l=0,r=(n-1)-x,ret=-1;
    pair<int,int> c;
    bool br=0;
    while(l<=r&&!br){
        int mid=l+(r-l)/2;
        if(l==r){
            br=1;
            c=cal(x,x+l);
        }else{
            c=cal(x,x+mid);
        }
        if(-c.first>c.second){
            r=mid-1;
            continue;
        }
        if(-c.first<c.second){
            l=mid+1;
            continue;
        }
        if(-c.first==c.second){
            l=mid+1;
            ret=max(ret,mid);
            continue;
        }
    }
    return ret;
}
int bs_first(int x){
    int l=0,r=(n-1)-x,ret=1e9;
    pair<int,int> c;
    bool br=0;
    while(l<=r&&!br){
        int mid=l+(r-l)/2;
        if(l==r){
            br=1;
            c=cal(x,x+l);
        }else{
            c=cal(x,x+mid);
        }
        if(-c.first>c.second){
            r=mid-1;
            continue;
        }
        if(-c.first<c.second){
            l=mid+1;
            continue;
        }
        if(-c.first==c.second){
            r=mid-1;
            ret=min(ret,mid);
            continue;
        }
    }
    return ret;
}

int main(){
    scanf("%d",&n);
    int si=-1;
    for(int j=0;j<2;j++){
        for(int i=0;i<n;i++){
            scanf("%d",&S[j].a[i]);
            S[j].a[i]*=si;
        }
        si=1;
        S[j].build(1,0,n-1);
    }
    long long ans=0;
    pair<int,int> range;
    for(int i=0;i<n;i++){
        range.first=bs_first(i);
        range.second=bs_last(i);
        if(range.first==1e9||range.second==-1){
            continue;
        }
        ans+=range.second-range.first+1;
    }

    printf("%lld\n",ans);
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;

int dx[8]={1,-1,0,0,1,-1,1,-1};
int dy[8]={0,0,-1,1,1,-1,-1,1};
class SegmentTree{
public:
    int ans,a[200000],st[800000],i,j;
    void pull(int idx){
        st[idx]=min(st[idx<<1],st[(idx<<1)+1]);
    }
    void build(int idx,int l,int r){
        if(l==r){
            st[idx]=a[l];
            return ;
        }
        build(idx<<1,l,(l+r)>>1);
        build((idx<<1)+1,(l+r)/2+1,r);
        pull(idx);
    }
    void get(int idx,int l,int r){
        if(l>j||r<i){
            return ;
        }
        if(l>=i&&r<=j){
            ans=min(ans,st[idx]);
            return ;
        }
        get(idx<<1,l,(l+r)>>1);
        get((idx<<1)+1,(l+r)/2+1,r);
    }
};
int n ;
SegmentTree S[2];

int bs1(int x,int y){
    int ret=-1;
    int l=1,r=n-1-y;
    int si=1;
    if(!x){si=-1;}
    while(l<=r){
        int mid=(l+r)/2;
        S[x].ans=1e9;S[x].ans+=99;
        S[x].i=y;S[x].j=y+mid;
        S[x].get(1,0,n-1);
        if(l+1 == r){
            S[x].ans=1e9;S[x].ans+=99;
            S[x].i=y;S[x].j=y+l;
            S[x].get(1,0,n-1);
            if(S[x].ans*si==S[1].a[y]){
                ret=max(ret,l);
            }
            l++;
            break;
        }
        if(S[x].ans*si==S[1].a[y]){
            ret=mid;
            l=mid+1;
        }else{
            r=mid-1;
        }
    }
    return ret;
}
int bs2(int x,int y){
    int ret=-1;
    int l=1,r=y;
    int si=1;
    if(!x){si=-1;}
    while(l<=r){
        int mid=(l+r)/2;
        S[x].ans=1e9;S[x].ans+=99;
        S[x].j=y;S[x].i=y-mid;
        S[x].get(1,0,n-1);
        if(l+1 == r){
            S[x].ans=1e9;S[x].ans+=99;
            S[x].j=y;S[x].i=y-l;
            S[x].get(1,0,n-1);
            if(S[x].ans*si==S[1].a[y]){
                ret=max(ret,l);
            }
            l++;
            break;
        }
        if(S[x].ans*si==S[1].a[y]){
            ret=mid;
            l=mid+1;
        }else{
            r=mid-1;
        }
    }
    return ret;
}

int main(){
    scanf("%d",&n);
    int si=-1;
    for(int j=0;j<2;j++){
        for(int i=0;i<n;i++){
            scanf("%d",&S[j].a[i]);
            S[j].a[i]*=si;
        }
        si=1;
        S[j].build(1, 0, n-1);
    }
    long long ans=0;
    for(int i=0;i<n;i++){
        int rr=min(bs1(0,i),bs1(1,i));
        int ll=min(bs2(0,i),bs2(1,i));
        //cout<<i<<" "<<rr<<" "<<ll<<endl;
        if(-S[0].a[i]==S[1].a[i]){
            ans++;
        }
        if(rr!=-1){
            ans+=rr;
        }
        if(ll!=-1){
            ans+=ll;
        }
        if(ll>0&&rr>0){
            ans+=1LL*ll*rr;
        }

    }
    printf("%lld\n",ans);

    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>
#include <complex>

using namespace std;
int n , a[200000],b[200000],st[2][18][200000],low,high,ll,rr,mx,mn,lg[200001];
long long ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        lg[i]=log2(i);
        if(1<<lg[i] > i){
            lg[i]--;
        }
    }
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&b[i]);
    }
    for(int i=0;i<n;i++){
        st[0][0][i]=a[i];
        st[1][0][i]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=0;i<n;i++){
            st[0][j][i]=st[0][j-1][i];
            st[1][j][i]=st[1][j-1][i];
            if(i+(1<<(j-1))<n){
                st[0][j][i]=max(st[0][j][i],st[0][j-1][i+(1<<(j-1))]);
                st[1][j][i]=min(st[1][j][i],st[1][j-1][i+(1<<(j-1))]);
            }

        }
    }
 //   cout<<st[0][lg[2]][0]<<endl;
    bool br=0;
    int idx[2];
    for(int i=0;i<n;i++){
        idx[0]=idx[1]=-1;
        ll=i,rr=n-1;
        br=0;
        while(!br&&ll<=rr){
            mx=-1e9 - 9,mn=1e9+9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=(mid-i)+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-lg[len]-1]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-lg[len]-1]);
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                 continue;
            }
            if(mx==mn){
                idx[0]=mid;
                rr=mid-1;
            }
        }
        br=0;
        ll=i,rr=n-1;
        while(!br&&ll<=rr){
            mx=-1e9 - 9,mn=1e9+9;
            int mid=ll+(rr-ll)/2;
            if(ll==rr){
                br=1;
            }
            int len=mid-i+1;
            mx=max(st[0][lg[len]][i],st[0][lg[len]][i+len-lg[len]-1]);
            mn=min(st[1][lg[len]][i],st[1][lg[len]][i+len-lg[len]-1]);
            if(mx>mn){
                rr=mid-1;
                continue;
            }
            if(mx<mn){
                ll=mid+1;
                continue;

            }
            if(mx==mn){
                idx[1]=mid;
                ll=mid+1;
                continue;

            }
        }
       // cout<<idx[0]<<" "<<idx[1]<<endl;
        if(idx[0]==-1){continue;}
        ans+=idx[1]-idx[0]+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	mn[0][n]=b[n];
	mx[0][n]=a[n];
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mar=i,mir=i,mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])>=max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		if(mar==mir){
			if(a[mir]==b[mir]){
				ans++;
			}
		}
		else if(mir==i){
			if(a[i]<=mxx && b[i]>=mnn){
				ans+=(mar-mir+1);
			}
			else{
				ans+=(mar-mir);
			}
		}
		else{
			ans+=(mar-mir);
		}
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	//mn[0][n]=b[n];
	//mx[0][n]=a[n];
	for(int i=n;i>=1;i--){
		mn[0][i]=b[i];
		mx[0][i]=a[i];
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mir=i,mxx=-INF,mnn=INF;
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n+1){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		mxx=-INF,mnn=INF;
		ll mar=mir;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n+1){
				continue;
			}
			if(min(mnn,mn[j][mar])==max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		/*if(mar==mir){
			if(a[mir]==b[mir]){
				ans++;
			}
		}
		else if(mir==i){
			if(a[i]<=mxx && b[i]>=mnn){
				ans+=(mar-mir+1);
			}
			else{
				ans+=(mar-mir);
			}
		}
		else{
			ans+=(mar-mir);
		}*/
		ans+=mar-mir;
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	//mn[0][n]=b[n];
	//mx[0][n]=a[n];
	for(int i=n;i>=1;i--){
		mn[0][i]=b[i];
		mx[0][i]=a[i];
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mir=i,mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n+1){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		mxx=a[i],mnn=b[i];
		ll mar=mir;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n+1){
				continue;
			}
			if(min(mnn,mn[j][mar])==max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		/*if(mar==mir){
			if(a[mir]==b[mir]){
				ans++;
			}
		}
		else if(mir==i){
			if(a[i]<=mxx && b[i]>=mnn){
				ans+=(mar-mir+1);
			}
			else{
				ans+=(mar-mir);
			}
		}
		else{
			ans+=(mar-mir);
		}*/
		ans+=mar-mir;
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	mn[0][n]=b[n];
	mx[0][n]=a[n];
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mar=i,mir=i,mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])>=max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		if(mar>i){
			if(mir<mar){
				ans+=(mar-mir+1);
			}
			else{
				if(a[mir]==b[mir]){
					ans++;
				}
			}
		}
		else if(a[i]==b[i]){
			ans++;
		}
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<=n;i++){
		int mar=i;
		int mir=i;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]==mx[j][mar]){
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		if(mar>mir){
			//cout<<mar<<" "<<mir<<endl;
			ans+=mar-mir+1;
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	//mn[0][n]=b[n];
	//mx[0][n]=a[n];
	for(int i=n-1;i>=1;i--){
		mn[0][i]=b[i];
		mx[0][i]=a[i];
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mir=i,mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		mxx=a[i],mnn=b[i];
		ll mar=mir;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])==max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		/*if(mar==mir){
			if(a[mir]==b[mir]){
				ans++;
			}
		}
		else if(mir==i){
			if(a[i]<=mxx && b[i]>=mnn){
				ans+=(mar-mir+1);
			}
			else{
				ans+=(mar-mir);
			}
		}
		else{
			ans+=(mar-mir);
		}*/
		ans+=mar-mir;
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<=n;i++){
		int mar=i;
		int mir=i;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]==mx[j][mar]){
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		if(mar!=i){
			//cout<<mar<<" "<<mir<<endl;
			ans+=mar-mir+1;
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	mn[0][n]=b[n];
	mx[0][n]=a[n];
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mar=i,mir=i,mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])>=max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		if(mar>mir){
			ans+=mar-mir;
		}
		else if(a[mir+1]==b[mir+1] && mir+1<=n){
			ans++;
		}
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	mn[0][n]=b[n];
	mx[0][n]=a[n];
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mar=i,mir=i,mxx=-INF,mnn=INF;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]>=mx[j][mar]){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<endl;
		if(mxx==mnn){
			ans+=mar-mir+1;
		}
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	mn[0][n]=b[n];
	mx[0][n]=a[n];
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mar=i,mir=i,mxx=-INF,mnn=INF;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])>=max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		mxx=-INF,mnn=INF;
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])>max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		if(mar>mir){
			ans+=mar-mir;
		}
		else if(a[mir]==b[mir]){
			ans++;
		}
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<=n;i++){
		int mar=i;
		int mir=i;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]==mx[j][mar]){
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		if(mar>=mir){
			if(mar!=i){
				ans--;
			}
			//cout<<mar<<" "<<mir<<endl;
			ans+=mar-mir+1;
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	//mn[0][n]=b[n];
	//mx[0][n]=a[n];
	for(int i=n;i>=1;i--){
		mn[0][i]=b[i];
		mx[0][i]=a[i];
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mir=i,mxx=-INF,mnn=INF;
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>(n+1)){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		ll mar=mir;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>(n+1)){
				continue;
			}
			if(min(mnn,mn[j][mar])==max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		/*if(mar==mir){
			if(a[mir]==b[mir]){
				ans++;
			}
		}
		else if(mir==i){
			if(a[i]<=mxx && b[i]>=mnn){
				ans+=(mar-mir+1);
			}
			else{
				ans+=(mar-mir);
			}
		}
		else{
			ans+=(mar-mir);
		}*/
		ans+=mar-mir;
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<=n;i++){
		int mar=i;
		int mir=i;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]>=mx[j][mar]){
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		if(mar>=mir){
			ans+=mar-mir;
		}
		if(a[mir]==b[mir]){
			ans++;
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<=n;i++){
		int mar=i;
		int mir=i;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]==mx[j][mar]){
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		if(mar>=mir){
			if(mar==i){
				ans--;
			}
			//cout<<mar<<" "<<mir<<endl;
			ans+=mar-mir+1;
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=n-1;i>=1;i--){
		mn[0][i]=min(b[i+1],b[i]);
		mx[0][i]=max(a[i+1],a[i]);
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<=n;i++){
		int mar=i;
		int mir=i;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mar]>=mx[j][mar]){
				mar+=(1ll<<j);
			}
		}
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(mn[j][mir]>mx[j][mir]){
				mir+=(1ll<<j);
			}
		}
		if(mar>mir){
			ans+=mar-mir+1;
		}
		else if(a[mar]==b[mar]){
			ans++;
		}
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+100,INF=1ll<<60;
ll a[maxn],b[maxn],mx[19][maxn],mn[19][maxn],ans;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	//mn[0][n]=b[n];
	//mx[0][n]=a[n];
	for(int i=n;i>=1;i--){
		mn[0][i]=b[i];
		mx[0][i]=a[i];
		for(int j=1;j<=18;j++){
			mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1ll<<(j-1))]);
			mx[j][i]=max(mx[j-1][i],mx[j-1][i+(1ll<<(j-1))]);
		}
	}
	//cout<<mx[0][1]<<" "<<mx[1][1]<<endl;
	for(int i=1;i<n;i++){
		ll mir=i,mxx=a[i],mnn=b[i];
		for(int j=18;j>=0;j--){
			if((mir+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mir])>max(mxx,mx[j][mir])){
				mxx=max(mxx,mx[j][mir]);
				mnn=min(mnn,mn[j][mir]);
				mir+=(1ll<<j);
			}
		}
		mxx=a[i],mnn=b[i];
		ll mar=mir;
		for(int j=18;j>=0;j--){
			if((mar+(1ll<<j))>n){
				continue;
			}
			if(min(mnn,mn[j][mar])==max(mxx,mx[j][mar])){
				mxx=max(mxx,mx[j][mar]);
				mnn=min(mnn,mn[j][mar]);
				mar+=(1ll<<j);
			}
		}
		//cout<<mar<<" "<<mir<<" "<<mxx<<" "<<mnn<<endl;
		/*if(mar==mir){
			if(a[mir]==b[mir]){
				ans++;
			}
		}
		else if(mir==i){
			if(a[i]<=mxx && b[i]>=mnn){
				ans+=(mar-mir+1);
			}
			else{
				ans+=(mar-mir);
			}
		}
		else{
			ans+=(mar-mir);
		}*/
		ans+=mar-mir;
	}
	if(a[n]==b[n]){
		ans++;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
#define mid (l+r>>1)
const int N = 2e5+9 , inf = 2e9 ;
int a[N],b[N],n;
pii st[N<<2];
pii operate(pii a,pii b)
{
    return {max(a.fi,b.fi),min(a.se,b.se)};
}
void construct(int l,int r,int m)
{
    if(l==r)
    {
        st[m]={a[l],b[l]};
        return ;
    }
    construct(l,mid,m<<1);
    construct(mid+1,r,m<<1|1);
    st[m]=operate(st[m<<1],st[m<<1|1]);
}
pii get(int l,int r,int x,int y,int m)
{
    if(r<x||l>y)return {-inf,inf};
    if(l>=x&&r<=y)return st[m];
    return operate(get(l,mid,x,y,m<<1),get(mid+1,r,x,y,m<<1|1));
}
int getr(int idx)
{
    int l=1,r=idx+1;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>=ret.se)l=mid;
        else r=mid;
    }
    pii ret = get(1,n,l,l,1);
    if(ret.fi!=ret.se)return inf ;
    return l ;
}
int getl(int idx)
{
    //return 0 ;
    int l=0,r=idx;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>ret.se)l=mid;
        else r=mid;
    }
    return r ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i] ;
    }
    for(int i=1;i<=n;i++)
    {
        cin >> b[i] ;
    }
    construct(1,n,1);
    long long ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        int ret = getr(i)-getl(i)+1;
        if(ret<=n)ans+=ret ;
        //printf("check %d %lld\n",ret,ans);
        //return 0 ;
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
#define mid (l+r>>1)
const int N = 2e5+9 , inf = 2e9 ;
int a[N],b[N],n;
pii st[N<<2];
pii operate(pii a,pii b)
{
    return {max(a.fi,b.fi),min(a.se,b.se)};
}
void construct(int l,int r,int m)
{
    if(l==r)
    {
        st[m]={a[l],b[l]};
        return ;
    }
    construct(l,mid,m<<1);
    construct(mid+1,r,m<<1|1);
    st[m]=operate(st[m<<1],st[m<<1|1]);
}
pii get(int l,int r,int x,int y,int m)
{
    if(r<x||l>y)return {-inf,inf};
    if(l>=x&&r<=y)return st[m];
    return operate(get(l,mid,x,y,m<<1),get(mid+1,r,x,y,m<<1|1));
}
int getr(int idx)
{
    int l=1,r=idx+1;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>=ret.se)l=mid;
        else r=mid;
    }
    pii ret = get(1,n,l,l,1);
    if(ret.fi!=ret.se)return inf ;
    return l ;
}
int getl(int idx)
{
    //return 0 ;
    int l=0,r=idx;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>ret.se)l=mid;
        else r=mid;
    }
    pii ret = get(1,n,r,r,1);
    if(ret.fi!=ret.se)return -inf ;
    return r ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i] ;
    }
    for(int i=1;i<=n;i++)
    {
        cin >> b[i] ;
    }
    construct(1,n,1);
    long long ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        long long ret = getr(i)-getl(i)+1;
        if(ret<=1ll*n)ans+=ret ;
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
#define mid (l+r>>1)
const int N = 2e5+9 , inf = 2e9 ;
int a[N],b[N],n;
pii st[N<<2];
pii operate(pii a,pii b)
{
    return {max(a.fi,b.fi),min(a.se,b.se)};
}
void construct(int l,int r,int m)
{
    if(l==r)
    {
        st[m]={a[l],b[l]};
        return ;
    }
    construct(l,mid,m<<1);
    construct(mid+1,r,m<<1|1);
    st[m]=operate(st[m<<1],st[m<<1|1]);
}
pii get(int l,int r,int x,int y,int m)
{
    if(r<x||l>y)return {-inf,inf};
    if(l>=x&&r<=y)return st[m];
    return operate(get(l,mid,x,y,m<<1),get(mid+1,r,x,y,m<<1|1));
}
long long getr(int idx)
{
    int l=1,r=idx+1;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>=ret.se)l=mid;
        else r=mid;
    }
    while(l>idx||l<1);
    pii ret = get(1,n,l,idx,1);
    if(ret.fi!=ret.se)return inf ;
    return l ;
}
long long getl(int idx)
{
    //return 0 ;
    int l=0,r=idx;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>ret.se)l=mid;
        else r=mid;
    }
    while(r>idx||r<1);
    pii ret = get(1,n,r,idx,1);
    if(ret.fi!=ret.se)return -inf ;
    return r ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i] ;
    }
    for(int i=1;i<=n;i++)
    {
        cin >> b[i] ;
    }
    construct(1,n,1);
    long long ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        long long  R = getr(i), L = getl(i) ;
        while(L>R);
        long long ret = R-L+1;
        if(ret<=1ll*n)ans+=ret ;
        //printf("check %lld %lld \n",L,R);
    }
    //printf("check max %d \n",get(1,n,1,3,1).se);
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
#define mid (l+r>>1)
const int N = 2e5+9 , inf = 2e9 ;
int a[N],b[N],n;
pii st[N<<2];
pii operate(pii a,pii b)
{
    return {max(a.fi,b.fi),min(a.se,b.se)};
}
void construct(int l,int r,int m)
{
    if(l==r)
    {
        st[m]={a[l],b[l]};
        return ;
    }
    construct(l,mid,m<<1);
    construct(mid+1,r,m<<1|1);
    st[m]=operate(st[m<<1],st[m<<1|1]);
}
pii get(int l,int r,int x,int y,int m)
{
    if(r<x||l>y)return {-inf,inf};
    if(l>=x&&r<=y)return st[m];
    return operate(get(l,mid,x,y,m<<1),get(mid+1,r,x,y,m<<1|1));
}
long long getr(int idx)
{
    int l=1,r=idx+1;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>=ret.se)l=mid;
        else r=mid;
    }
    while(l>idx||l<1);
    pii ret = get(1,n,l,l,1);
    if(ret.fi!=ret.se)return inf ;
    return l ;
}
long long getl(int idx)
{
    //return 0 ;
    int l=0,r=idx;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>ret.se)l=mid;
        else r=mid;
    }
    while(r>idx||r<1);
    pii ret = get(1,n,r,r,1);
    if(ret.fi!=ret.se)return -inf ;
    return r ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i] ;
    }
    for(int i=1;i<=n;i++)
    {
        cin >> b[i] ;
    }
    construct(1,n,1);
    long long ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        long long  R = getr(i), L = getl(i) ;
        while(L>R);
        long long ret = R-L+1;
        if(ret<=1ll*n)ans+=ret ;
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
#define mid (l+r>>1)
const int N = 2e5+9 , inf = 2e9 ;
int a[N],b[N],n;
pii st[N<<2];
pii operate(pii a,pii b)
{
    return {max(a.fi,b.fi),min(a.se,b.se)};
}
void construct(int l,int r,int m)
{
    if(l==r)
    {
        st[m]={a[l],b[l]};
        return ;
    }
    construct(l,mid,m<<1);
    construct(mid+1,r,m<<1|1);
    st[m]=operate(st[m<<1],st[m<<1|1]);
}
pii get(int l,int r,int x,int y,int m)
{
    if(r<x||l>y)return {-inf,inf};
    if(l>=x&&r<=y)return st[m];
    return operate(get(l,mid,x,y,m<<1),get(mid+1,r,x,y,m<<1|1));
}
long long getr(int idx)
{
    int l=1,r=idx+1;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>=ret.se)l=mid;
        else r=mid;
    }
    pii ret = get(1,n,l,l,1);
    if(ret.fi!=ret.se)return inf ;
    return l ;
}
long long getl(int idx)
{
    //return 0 ;
    int l=0,r=idx;
    while(l+1<r)
    {
        pii ret = get(1,n,mid,idx,1);
        if(ret.fi>ret.se)l=mid;
        else r=mid;
    }
    pii ret = get(1,n,r,r,1);
    if(ret.fi!=ret.se)return -inf ;
    return r ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i] ;
    }
    for(int i=1;i<=n;i++)
    {
        cin >> b[i] ;
    }
    construct(1,n,1);
    long long ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        long long ret = getr(i)-getl(i)+1;
        if(ret<=1ll*n)ans+=ret ;
    }
    cout << ans << '\n' ;
    return 0 ;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>a(200005),b(200005);
int sparse1[200005][21],sparse2[200005][21];
int n;
map<pair<int,int> ,int> p,q;
void build()
{
	for(int i=0;i<n;i++)
	{
		sparse1[i][0]=a[i];
		sparse2[i][0]=b[i];
	}
	for(int j=1;j<=18;j++)
	{
		for(int i=0;i<=(n-(1LL<<j));i++)
		{
			sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]);
			sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int query1(int l,int r)
{
	int ans=-inf;
	for(int j=18;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=max(ans,sparse1[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int query2(int l,int r)
{
	int ans=inf;
	for(int j=19;j>=0;j--)
	{
		if(l+(1LL<<j)-1<=r)
		{
			ans=min(ans,sparse2[l][j]);
			l=l+(1LL<<j);
		}
	}
	return ans;
}
int32_t main()
{
   IOS;
   // #ifndef ONLINE_JUDGE
   // freopen("input.txt","r",stdin);
   // freopen("output.txt","w",stdout);
   // #endif
   /********************* Code starts here ***********************/
   cin>>n;
   for(int i=0;i<n;i++)
   {
   	cin>>a[i];
   }
   for(int i=0;i<n;i++)
   {
   	cin>>b[i];
   }
   build();
   int ans=0;
  // cout<<n<<endl;
   for(int i=0;i<n;i++)
   {
   	int l=i,h=n-1,L1=inf,L2=-inf;
   	while(l<h)
   	{
   		int mid=(l+h+1)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L2=max(L2,mid);
   			l=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid-1;
   		}
   		else
   		{
   			l=mid;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	int c=l;
   	l=i,h=n-1;
   	while(l<h)
   	{
   		int mid=(l+h)/2;
   		int mi;
   		mi=query2(i,mid);
   		int ma;
   		ma=query1(i,mid);
   		if(ma==mi)
   		{
   			L1=min(L1,mid);
   			h=mid;
   		}
   		else if(ma>mi)
   		{
   			h=mid;
   		}
   		else
   		{
   			l=mid+1;
   		}
   	}
   	if(query2(i,l)!=query1(i,l))
   		continue;
   	c=c-l+1;
   	ans=ans+c;
   }
  cout<<ans<<endl;
}