#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IO ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
const int INF = 2e9;
const int MAX = 2e5;
const int MV = 1e5;
const int MOD = 1e9 + 7;
const int LG = 20;
int MA = -INF , MI = INF , ANS;

int n , arr[2][MAX+10];
int mn[LG][MAX+10] , mx[LG][MAX+10] , lg[MAX+10];

void cal()
{
	for(int i=2 ; i<=n ; i++)
		lg[i] = lg[i/2] + 1;
	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr[1][i] , mx[0][i] = arr[0][i];

	int tmp;
	for(int h = 1 ; h < LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			tmp = i + (1<<(h-1));
			if( tmp > n )
				mn[h][i] = mn[h-1][i] , mx[h][i] = mx[h-1][i];
			else
				mn[h][i] = min( mn[h-1][i] , mn[h-1][tmp] ) ,
				mx[h][i] = max( mx[h-1][i] , mx[h-1][tmp] );
		}
	}
}

int get_mx( int be , int en )
{
	return max( mx[ lg[en-be+1] ][be] , mx[ lg[en-be+1] ][ en - ( 1 << (lg[en-be+1]) ) + 1 ] );
}

int get_mn( int be , int en )
{
	return min( mn[ lg[en-be+1] ][be] , mn[ lg[en-be+1] ][ en - ( 1 << (lg[en-be+1]) ) + 1 ] );
}

int32_t main()
{
	IO;
	cin >> n;

	memset( mn , 31 , sizeof mn );
	memset( mx , -31 , sizeof mx );

	for(int i=1 ; i<=n ; i++)
		cin >> arr[0][i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr[1][i];

	cal();
	for(int i=1 ; i<=n ; i++)
	{
		int l = i-1 , r = i;

		if( get_mx(i,i) > get_mn(i,i) )
			continue;

		for(int k = LG-1 ; k >= 0 ; k-- )
		{
			if( ( l + (1<<k) ) <= n &&  get_mx( i , l + (1<<k) ) < get_mn( i , l + (1<<k) ) )
				l += ( 1<<k );
			if( ( r + (1<<k) ) <= n &&  get_mx( i , r + (1<<k) ) <= get_mn( i , r + (1<<k)) )
				r += ( 1<<k );
		}
	//	cerr << l << " " << r <<" || " << i << "\n";
		ANS += r-l;
	}

	cout << ANS << "\n";
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const ll INF = 4e18;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll MA = -INF , MI = INF , ANS;

ll n , arr1[MAX+10] , arr2[MAX+10];
ll lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);

		}
	}
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<n ; i++)
	{
		int l = i;
		MA = -INF , MI = INF;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( l + (1<<k) <= n+1 && ( min( MI , mn[k][l] ) > max( MA , mx[k][l] ) ) )
				MI = min( MI , mn[k][l] ) , MA = max( MA , mx[k][l] ) , l += (1<<k);
		}

		int r = l;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( r + (1<<k) <= n+1 && ( min( MI , mn[k][r] ) == max( MA , mx[k][r] ) ) )
				MI = min( MI , mn[k][r] ) , MA = max( MA , mx[k][r] ) , r += (1<<k);
		}
		ANS += r-l;
	}
	if( arr1[n] == arr2[n] )
		ANS++;
	cout<<ANS;
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const ll INF = 4e18;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll MA = -INF , MI = INF , ANS;

ll n , arr1[MAX+10] , arr2[MAX+10];
ll lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n+1 ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);
		}
	}
}

int get( int l , int r , int type )
{
	int tmp = ( 1 << ( lg[r-l] ) );
	if( type == 0 )
		return min( mn[ lg[r-l] ][l] , mn[ lg[r-l] ][ r-tmp ] );
	else
		return max( mx[ lg[r-l] ][l] , mx[ lg[r-l] ][ r-tmp ] );
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	memset( mx , -63 , sizeof mx );
	memset( mn , 63 , sizeof mn );

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<=n ; i++)
	{
		int l = i-1 , r = i;
		if( get(i,i+1,0) < get(i,i+1,1) )
			continue;

		for(int k = LG-1 ; k>=0 ; k--)
		{
			if( ( l + (1<<k) ) <= n+1 && ( get( i,l + ( 1<<k ),0 ) > get( i,l + (1<<k),1 ) ) )
				l += ( 1 << k );
			if( ( r + (1<<k) ) <= n+1 && ( get( i , r + (1<<k) , 0 ) == get(i,r+(1<<k) , 1 ) ) )
				r += ( 1 << k );
		}
	//	cerr << i <<" || "<<l<<" "<<r<<"\n";
		if( l == i-1 && r == i )
		{
			ANS++;
			continue;
		}
		if( r < l )
			continue;
		if( r > i )
			r--;
		if( l==i-1)
			l++;
	//	cerr << " after " << l<<" "<<r<<"\n";
		ANS += r-l+1;
	}
	cout << ANS <<"\n";
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const ll INF = 4e18;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll MA = -INF , MI = INF , ANS;

ll n , arr1[MAX+10] , arr2[MAX+10];
ll lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n+2 ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);
		}
	}
}

int get( int l , int r , int type )
{
	int tmp = ( 1 << ( lg[r-l] ) );
	if( type == 0 )
		return min( mn[ lg[r-l] ][l] , mn[ lg[r-l] ][ r-tmp ] );
	else
		return max( mx[ lg[r-l] ][l] , mx[ lg[r-l] ][ r-tmp ] );
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	memset( mx , -63 , sizeof mx );
	memset( mn , 63 , sizeof mn );

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<=n ; i++)
	{
		int l = i-1 , r = i-1;
		if( get(i,i+1,0) < get(i,i+1,1) )
			continue;

		for(int k = LG-1 ; k>=0 ; k--)
		{
			if( ( l + (1<<k) ) <= n && ( get( i,l + ( 1<<k )+1, 0 ) > get( i,l+(1<<k)+1,1 ) ) )
				l += ( 1 << k );
			if( ( r + (1<<k) ) <= n && ( get( i , r + (1<<k) + 1, 0 ) == get(i,r+(1<<k)+1 , 1 ) ) )
				r += ( 1 << k );
		}
//		cerr << i <<" || "<<l<<" "<<r<<"\n";
		if( r == i && mn[0][i] != mx[0][i] )
			continue;
		if( r < l )
			continue;

//		cerr << " after " << l<<" "<<r<<"\n";
		ANS += r-l;
	}
	cout << ANS <<"\n";
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const int INF = 2e9;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
int MA = -INF , MI = INF , ANS;

int n , arr1[MAX+10] , arr2[MAX+10];
int lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);

		}
	}
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<n ; i++)
	{
		int l = i;
		MA = -INF , MI = INF;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( l + (1<<k) <= n+1 && ( min( MI , mn[k][l] ) > max( MA , mx[k][l] ) ) )
				MI = min( MI , mn[k][l] ) , MA = max( MA , mx[k][l] ) , l += (1<<k);
		}

		int r = l;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( r + (1<<k) <= n+1 && ( min( MI , mn[k][r] ) == max( MA , mx[k][r] ) ) )
				MI = min( MI , mn[k][r] ) , MA = max( MA , mx[k][r] ) , r += (1<<k);
		}
		ANS += r-l;
	}
	if( arr1[n] == arr2[n] )
		ANS++;
	cout<<ANS;
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const ll INF = 4e18;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll MA = -INF , MI = INF , ANS;

ll n , arr1[MAX+10] , arr2[MAX+10];
ll lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);
		}
	}
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<n ; i++)
	{
		int l = i;
		MA = -INF , MI = INF;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( l + (1<<k) <= n+1 && ( min( MI , mn[k][l] ) > max( MA , mx[k][l] ) ) )
				MI = min( MI , mn[k][l] ) , MA = max( MA , mx[k][l] ) , l += (1<<k);
		}

		int r = l;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( r + (1<<k) <= n+1 && ( min( MI , mn[k][r] ) == max( MA , mx[k][r] ) ) )
				MI = min( MI , mn[k][r] ) , MA = max( MA , mx[k][r] ) , r += (1<<k);
		}
		ANS += r-l;
	}
	if( arr1[n] == arr2[n] )
		ANS++;
	cout<<ANS;
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const ll INF = 4e18;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll MA = -INF , MI = INF , ANS;

ll n , arr1[MAX+10] , arr2[MAX+10];
ll lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n+2 ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);
		}
	}
}

int get( int l , int r , int type )
{
	int tmp = ( 1 << ( lg[r-l+1] ) );
	if( type == 0 )
		return min( mn[ lg[r-l+1] ][l] , mn[ lg[r-l+1] ][ r-tmp+1 ] );
	else
		return max( mx[ lg[r-l+1] ][l] , mx[ lg[r-l+1] ][ r-tmp+1 ] );
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

//	memset( mx , -63 , sizeof mx );
//	memset( mn , 63 , sizeof mn );

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<=n ; i++)
	{
		int l = i-1 , r = i;
		if( get(i,i,0) < get(i,i,1) )
			continue;

		for(int k = LG-1 ; k>=0 ; k--)
		{
			if( ( l + (1<<k) ) <= n && ( get( i,l + ( 1<<k ), 0 ) > get( i,l+(1<<k),1 ) ) )
				l += ( 1 << k );
			if( ( r + (1<<k) ) <= n && ( get( i , r + (1<<k) , 0 ) >= get(i,r+(1<<k) , 1 ) ) )
				r += ( 1 << k );
		}
//		cerr << i <<" || "<<l<<" "<<r<<"\n";
//		cerr << " after " << l<<" "<<r<<"\n";
		ANS += r-l;
	}
	cout << ANS <<"\n";
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const ll INF = 4e18;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll MA = -INF , MI = INF , ANS;

ll n , arr1[MAX+10] , arr2[MAX+10];
ll lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n+1 ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);
		}
	}
}

int get( int l , int r , int type )
{
	int tmp = ( 1 << ( lg[r-l] ) );
	if( type == 0 )
		return min( mn[ lg[r-l] ][l] , mn[ lg[r-l] ][ r-tmp ] );
	else
		return max( mx[ lg[r-l] ][l] , mx[ lg[r-l] ][ r-tmp ] );
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	memset( mx , -63 , sizeof mx );
	memset( mn , 63 , sizeof mn );

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<=n ; i++)
	{
		int l = i-1 , r = i;
		if( get(i,i+1,0) < get(i,i+1,1) )
			continue;

		for(int k = LG-1 ; k>=0 ; k--)
		{
			if( ( l + (1<<k) ) <= n+1 && ( get( i,l + ( 1<<k ),0 ) > get( i,l + (1<<k),1 ) ) )
				l += ( 1 << k );
			if( ( r + (1<<k) ) <= n+1 && ( get( i , r + (1<<k) , 0 ) == get(i,r+(1<<k) , 1 ) ) )
				r += ( 1 << k );
		}
		cerr << i <<" || "<<l<<" "<<r<<"\n";
		if( l > i-1)
			l--;
		if( r > i )
			r--;
		if( r < l )
			continue;
		ANS += r-l;
	}
	cout << ANS <<"\n";
	return 0;
}


/* SH10 */
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define IN_OUT(n) ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cout << fixed << setprecision(n)
typedef pair<int,int> pii;
typedef pair<pii,int> tpi;
typedef pair<pii,pii> qpi;
typedef long long ll;
const int INF = 2e9;
const int MAX = 2e5;
const int MOD = 1e9 + 7;
const int LG = 20;
int MA = -INF , MI = INF , ANS;

int n , arr1[MAX+10] , arr2[MAX+10];
int lg[MAX+10] , mn[LG+5][MAX+10] , mx[LG+5][MAX+10];

int cal_rmq()
{
	for(int i=2 ; i<=n ; i++)
		lg[i] = lg[i/2] + 1;

	for(int i=1 ; i<=n ; i++)
		mn[0][i] = arr1[i] , mx[0][i] = arr2[i];

	for(int h=1 ; h<LG ; h++)
	{
		for(int i=1 ; i<=n ; i++)
		{
			int x = i + (1<<(h-1));
			if( x > n )
				mx[h][i] = mx[h-1][i] , mn[h][i] = mn[h-1][i];
			else
				mx[h][i] = max( mx[h-1][i] , mx[h-1][x] ) , mn[h][i] = min( mn[h-1][i] , mn[h-1][x]);

		}
	}
}

int32_t main()
{
	IN_OUT(n);
	cin >> n;

	for(int i=1 ; i<=n ; i++)
		cin >> arr1[i];

	for(int i=1 ; i<=n ; i++)
		cin >> arr2[i];

	cal_rmq();

	for(int i=1 ; i<=n ; i++)
	{
		int l = i;
		MA = -INF , MI = INF;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( min( MI , mn[k][l] ) - max( MA , mx[k][l] ) > 0 && l + (1<<k) <= n+1 )
				MI = min( MI , mn[k][l] ) , MA = max( MA , mx[k][l] ) , l += (1<<k);
		}

		int r = i;
		MA = -INF , MI = INF;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( min( MI , mn[k][r] ) - max( MA , mx[k][r] ) >= 0 && r + (1<<k) <= n+1 )
				MI = min( MI , mn[k][l] ) , MA = max( MA , mx[k][l] ) , r += (1<<k);
		}
		ANS += r-l;
	}
	cout<<ANS;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[200010][21];
int b[200010][21];
int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i][0];
  }
  for (int i = 0; (1 << i) < n; i++) {
    for (int j = 0; j + (1 << i) < n; j++) {
      a[j][i + 1] = max(a[j][i], a[j + (1 << i)][i]);
      b[j][i + 1] = min(b[j][i], b[j + (1 << i)][i]);
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i][0] == b[i][0]) {
      int aa = a[i][0];
      int bb = b[i][0];
      int cur = i;
      for (int j = 20; j >= 0; j--) {
        if (cur + (1 << j) - 1 >= n) continue;
        if (a[cur][j] == b[cur][j]) {
          aa = max(aa, a[cur][j]);
          bb = min(bb, b[cur][j]);
          cur += (1 << j) - 1;
        }
      }
      if (aa == bb) {
        res += cur - i + 1;
      }
    }
  }
  cout << res << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[200010][21];
int b[200010][21];
int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i][0];
  }
  for (int i = 0; (1 << i) < n; i++) {
    for (int j = 0; j + (1 << i) < n; j++) {
      a[j][i + 1] = max(a[j][i], a[j + (1 << i)][i]);
      b[j][i + 1] = min(b[j][i], b[j + (1 << i)][i]);
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i][0] <= b[i][0]) {
      int aa = a[i][0];
      int bb = b[i][0];
      int cur = i;
      int cur1 = i;
      for (int j = 20; j >= 0; j--) {
        if (cur + (1 << j) - 1 >= n) continue;
        if (a[cur][j] <= b[cur][j]) {
          cur += (1 << j);
        }
        if (a[cur1][j] < b[cur1][j]) {
          cur1 += (1 << j);
        }
      }
      res += cur - cur1;
    }
  }
  cout << res << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 200010;
const int inf = 1e9 + 10;

int a[N], b[N];
int n;
long long res;
int mxtree[N * 4];
int mntree[N * 4];

void build(int p, int l, int r) {
    if (l == r) {
        mxtree[p] = a[l];
        mntree[p] = b[l];
        return;
    }
    int mid = (l + r) / 2;
    build(p + p, l, mid);
    build(p + p + 1, mid + 1, r);
    mxtree[p] = max(mxtree[p + p], mxtree[p + p + 1]);
    mntree[p] = min(mntree[p + p], mntree[p + p + 1]);
}

pair<int, int> get(int p, int l, int r, int L, int R) {
    if (r < L || l > R) return make_pair(-inf, inf);
    if (L <= l && r <= R) return make_pair(mxtree[p], mntree[p]);
    int mid = (l + r) / 2;
    pair<int, int> lc = get(p + p, l, mid, L, R);
    pair<int, int> rc = get(p + p + 1, mid + 1, r, L, R);
    return make_pair(max(lc.first, rc.first), min(lc.second, rc.second));
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    build(1, 1, n);
    pair<int, int> t = get(1, 1, n, 4, 6);
    for (int i = 1; i <= n; i++) {
        int lo = i - 1, hi = n + 1, mid;
        pair<int, int> g;
        while (lo + 1 < hi) {
            int mid = (lo + hi) / 2;
            g = get(1, 1, n, i, mid);
            if (g.first <= g.second) {
                lo = mid;
            }
            else {
                hi = mid;
            }
        }
        int t = lo;
        g = get(1, 1, n, i, t);
        if (g.first == g.second) {
            lo = i - 1, hi = n + 1;
            while (lo + 1 < hi) {
                mid = (lo + hi) / 2;
                g = get(1, 1, n, i, mid);
                if (g.first >= g.second) {
                    hi = mid;
                }
                else {
                    lo = mid;
                }
            }
            res += t - hi + 1;
        }

    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[200010][21];
int b[200010][21];
int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i][0];
  }
  for (int i = 0; (1 << i) < n; i++) {
    for (int j = 0; j + (1 << i) < n; j++) {
      a[j][i + 1] = max(a[j][i], a[j + (1 << i)][i]);
      b[j][i + 1] = min(b[j][i], b[j + (1 << i)][i]);
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i][0] <= b[i][0]) {
      int aa = a[i][0];
      int bb = b[i][0];
      int cur = i;
      int cur1 = i;
      for (int j = 20; j >= 0; j--) {
        if (cur + (1 << j) - 1 >= n) continue;
        if (a[cur][j] <= b[cur][j]) {
          aa = max(aa, a[cur][j]);
          bb = min(bb, b[cur][j]);
          cur += (1 << j);
        }
        if (a[cur1][j] < b[cur1][j]) {
          cur1 += (1 << j);
        }
      }
      if (aa == bb) {
        cout << i << " " << cur1 << " " << cur << "\n";
        res += cur - cur1;
      }
    }
  }
  cout << res << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[200010][21];
int b[200010][21];
int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i][0];
  }
  for (int i = 0; (1 << i) < n; i++) {
    for (int j = 0; j + (1 << i) < n; j++) {
      a[j][i + 1] = max(a[j][i], a[j + (1 << i)][i]);
      b[j][i + 1] = min(b[j][i], b[j + (1 << i)][i]);
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i][0] <= b[i][0]) {
      int aa = a[i][0];
      int bb = b[i][0];
      int cur = i;
      int cur1 = i;
      for (int j = 20; j >= 0; j--) {
        if (cur + (1 << j) - 1 >= n) continue;
        if (a[cur][j] <= b[cur][j]) {
          aa = max(aa, a[cur][j]);
          bb = min(bb, b[cur][j]);
          cur += (1 << j);
        }
        if (a[cur1][j] < b[cur1][j]) {
          cur1 += (1 << j);
        }
      }
      if (aa == bb) {
        res += cur - cur1;
      }
    }
  }
  cout << res << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[200010][21];
int b[200010][21];
int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i][0];
  }
  for (int i = 0; (1 << i) < n; i++) {
    for (int j = 0; j + (1 << i) < n; j++) {
      a[j][i + 1] = max(a[j][i], a[j + (1 << i)][i]);
      b[j][i + 1] = min(b[j][i], b[j + (1 << i)][i]);
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i][0] == b[i][0]) {
      int aa = a[i][0];
      int bb = b[i][0];
      int cur = i;
      for (int j = 18; j > 0; j--) {
        if (cur > n) break;
        if (cur + (1 << j) - 1 >= n) continue;
        if (a[cur][j] != b[cur][j]) continue;
        aa = max(aa, a[cur][j]);
        bb = min(bb, b[cur][j]);
        cur += (1 << j);
      }
      res += cur - i;
    }
  }
  cout << res << "\n";
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 200010;
const int inf = 1e9 + 10;

int a[N], b[N];
int n;
long long res;
int mxtree[N * 4];
int mntree[N * 4];

void build(int p, int l, int r) {
    if (l == r) {
        mxtree[p] = a[l];
        mntree[p] = b[l];
        return;
    }
    int mid = (l + r) / 2;
    build(p + p, l, mid);
    build(p + p + 1, mid + 1, r);
    mxtree[p] = max(mxtree[p + p], mxtree[p + p + 1]);
    mntree[p] = min(mntree[p + p], mntree[p + p + 1]);
}

pair<int, int> get(int p, int l, int r, int L, int R) {
    if (r < L || l > R) return make_pair(-inf, inf);
    if (L <= l && r <= R) return make_pair(mxtree[p], mntree[p]);
    int mid = (l + r) / 2;
    pair<int, int> lc = get(p + p, l, mid, L, R);
    pair<int, int> rc = get(p + p + 1, mid + 1, r, L, R);
    return make_pair(max(lc.first, rc.first), min(lc.second, rc.second));
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    build(1, 1, n);
    pair<int, int> t = get(1, 1, n, 4, 6);
    for (int i = 1; i <= n; i++) {
        if (a[i] <= b[i]) {
            int lo = i - 1, hi = n + 1, mid;
            pair<int, int> g;
            while (lo + 1 < hi) {
                int mid = (lo + hi) / 2;
                g = get(1, 1, n, i, mid);
                if (g.first <= g.second) {
                    lo = mid;
                }
                else {
                    hi = mid;
                }
            }
            int t = lo;
            g = get(1, 1, n, i, t);
            if (g.first == g.second) {
                lo = i - 1, hi = n + 1;
                while (lo + 1 < hi) {
                    mid = (lo + hi) / 2;
                    g = get(1, 1, n, i, mid);
                    if (g.first >= g.second) {
                        hi = mid;
                    }
                    else {
                        lo = mid;
                    }
                }
                res += t - hi + 1;
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 200010;
const int inf = 1e9 + 10;

int a[N], b[N];
int n, res;
int mxtree[N * 4];
int mntree[N * 4];

void build(int p, int l, int r) {
    if (l == r) {
        mxtree[p] = a[l];
        mntree[p] = b[l];
        return;
    }
    int mid = (l + r) / 2;
    build(p + p, l, mid);
    build(p + p + 1, mid + 1, r);
    mxtree[p] = max(mxtree[p + p], mxtree[p + p + 1]);
    mntree[p] = min(mntree[p + p], mntree[p + p + 1]);
}

pair<int, int> get(int p, int l, int r, int L, int R) {
    if (r < L || l > R) return make_pair(-inf, inf);
    if (L <= l && r <= R) return make_pair(mxtree[p], mntree[p]);
    int mid = (l + r) / 2;
    pair<int, int> lc = get(p + p, l, mid, L, R);
    pair<int, int> rc = get(p + p + 1, mid + 1, r, L, R);
    return make_pair(max(lc.first, rc.first), min(lc.second, rc.second));
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    build(1, 1, n);
    pair<int, int> t = get(1, 1, n, 4, 6);
    for (int i = 1; i <= n; i++) {
        if (a[i] <= b[i]) {
            int lo = i - 1, hi = n + 1, mid;
            pair<int, int> g;
            while (lo + 1 < hi) {
                int mid = (lo + hi) / 2;
                g = get(1, 1, n, i, mid);
                if (g.first <= g.second) {
                    lo = mid;
                }
                else {
                    hi = mid;
                }
            }
            int t = lo;
            g = get(1, 1, n, i, t);
            if (g.first == g.second) {
                lo = i - 1, hi = n + 1;
                while (lo + 1 < hi) {
                    mid = (lo + hi) / 2;
                    g = get(1, 1, n, i, mid);
                    if (g.first >= g.second) {
                        hi = mid;
                    }
                    else {
                        lo = mid;
                    }
                }
                res += t - hi + 1;
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
const int N=200100;
#define ll long long
using namespace std;
int Max[N][22],Min[N][20],n;
ll ans=0;
inline int qmax(int l,int r){
	int x=log2(r-l+1);
	return max(Max[l][x],Max[r-(1<<x)+1][x]);
}
inline int qmin(int l,int r){
	int x=log2(r-l+1);
	return min(Min[l][x],Min[r-(1<<x)+1][x]);
}
int main()
{
	int x,y;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Max[i][0]=x;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Min[i][0]=x;
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
		}
	}
	int l=1,r=1;
	for(int i=1;i<n;i++){
		l=i,r=n,y=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) y=mid,r=mid-1;
			else l=mid+1;
		}
		if(qmax(i,y)>qmin(i,y)) continue;
		l=i,r=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)>=qmax(i,mid)) x=mid,l=mid+1;
			else r=mid-1;
		}ans+=(x-y+1);
	}printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
const int N=200100;
#define ll long long
using namespace std;
int Max[N][22],Min[N][22],n;
ll ans=0;
inline int qmax(int l,int r){
	int x=log2(r-l+1);
	return max(Max[l][x],Max[r-(1<<x)+1][x]);
}
inline int qmin(int l,int r){
	int x=log2(r-l+1);
	return min(Min[l][x],Min[r-(1<<x)+1][x]);
}
int main()
{
//    freopen("1.in","r",stdin);
//    freopen("2.out","w",stdout);
	int x,y;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Max[i][0]=x;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Min[i][0]=x;
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
		}
	}
	int l=1,r=1;
	for(int i=1;i<=n;i++){
		l=i,r=n,y=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) y=mid,r=mid-1;
			else l=mid+1;
		}
		if(qmax(i,y)!=qmin(i,y)) continue;
		l=i,r=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)>=qmax(i,mid)) x=mid,l=mid+1;
			else r=mid-1;
		}ans+=(x-y+1);
	}printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
const int N=100100;
#define ll long long
using namespace std;
int Max[N][20],Min[N][20],n;
ll ans=0;
inline int qmax(int l,int r){
	int x=log2(r-l+1);
	return max(Max[l][x],Max[r-(1<<x)+1][x]);
}
inline int qmin(int l,int r){
	int x=log2(r-l+1);
	return min(Min[l][x],Min[r-(1<<x)+1][x]);
}
int main()
{
	int x,y;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Max[i][0]=x;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Min[i][0]=x;
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
		}
	}
	int l=1,r=1;
	for(int i=1;i<n;i++){
		l=i,r=n,y=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) y=mid,r=mid-1;
			else l=mid+1;
		}
		if(qmax(i,y)>qmin(i,y)) continue;
		l=i,r=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)>=qmax(i,mid)) x=mid,l=mid+1;
			else r=mid-1;
		}ans+=(x-y+1);
	}printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
const int N=100100;
#define ll long long
using namespace std;
int Max[N][20],Min[N][20],n;
ll ans=0;
inline int qmax(int l,int r){
	int x=log2(r-l+1);
	return max(Max[l][x],Max[r-(1<<x)+1][x]);
}
inline int qmin(int l,int r){
	int x=log2(r-l+1);
	return min(Min[l][x],Min[r-(1<<x)+1][x]);
}
int main()
{
	int x,y;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Max[i][0]=x;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Min[i][0]=x;
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
		}
	}
	int l=1,r=1;
	for(int i=1;i<n;i++){
		l=i,r=n,y=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) y=mid,r=mid-1;
			else l=mid+1;
		}
		if(qmax(i,y)>qmin(i,y)) continue;
		l=i+1,r=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) x=mid,l=mid+1;
			else r=mid-1;
		}ans+=(x-y+1);
	}printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
const int N=200100;
#define ll long long
using namespace std;
int Max[N][22],Min[N][22],n;
ll ans=0;
inline int qmax(int l,int r){
	int x=log2(r-l+1);
	return max(Max[l][x],Max[r-(1<<x)+1][x]);
}
inline int qmin(int l,int r){
	int x=log2(r-l+1);
	return min(Min[l][x],Min[r-(1<<x)+1][x]);
}
int main()
{
	int x,y;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Max[i][0]=x;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Min[i][0]=x;
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
		}
	}
	int l=1,r=1;
	for(int i=1;i<=n;i++){
		l=i,r=n,y=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) y=mid,r=mid-1;
			else l=mid+1;
		}
		if(qmax(i,y)>qmin(i,y)) continue;
		l=i,r=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)>=qmax(i,mid)) x=mid,l=mid+1;
			else r=mid-1;
		}ans+=(x-y+1);
	}printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
const int N=100100;
#define ll long long
using namespace std;
int Max[N][20],Min[N][20],n;
ll ans=0;
inline int qmax(int l,int r){
	int x=log2(r-l+1);
	return max(Max[l][x],Max[r-(1<<x)+1][x]);
}
inline int qmin(int l,int r){
	int x=log2(r-l+1);
	return min(Min[l][x],Min[r-(1<<x)+1][x]);
}
int main()
{
	int x,y;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Max[i][0]=x;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		Min[i][0]=x;
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
			Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
		}
	}
	int l=1,r=1;
	for(int i=1;i<n;i++){
		l=i,r=n,y=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)<=qmax(i,mid)) y=mid,r=mid-1;
			else l=mid+1;
		}
		if(qmax(i,y)>qmin(i,y)) continue;
		l=i+1,r=n;
		while(l<=r){
			int mid=(l+r)/2;
			if(qmin(i,mid)>=qmax(i,mid)) x=mid,l=mid+1;
			else r=mid-1;
		}ans+=(x-y+1);
	}printf("%lld\n",ans);
	return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[18][MAXN];
int table_b[18][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, n) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, n) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        cout << l << ' ' << r << endl;
        exit(0);
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second
#define int __int64

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
int Log[MAXN];
int table_a[20][MAXN];
int table_b[20][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

int get(int id) {
    int l = id - 1, r = n + 1;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(id, mid) >= getMin_for_b(id, mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    int LEFT = 0;
    if (getMax_for_a(id, r) == getMin_for_b(id, r)) {
        LEFT = r;
    } else {
        return 0;
    }
    l = id, r = n + 1;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(id, mid) > getMin_for_b(id, mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(id, l) == getMin_for_b(id, l)) {
        return l - LEFT + 1;
    } else {
        return 0;
    }
}

main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    ///...///
    for (int i = 1; i <= n; i++) {
        ans += get(i);
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[20][MAXN];
int table_b[20][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(id, mid) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(id, mid) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        cout << l << ' ' << r  << ' ' << id << endl;
        exit(0);
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    ///...///
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            //cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += (R-L+1);
        }
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[18][MAXN];
int table_b[18][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, n) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, n) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        cout << l << ' ' << r << endl;
        return 0;
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[18][MAXN];
int table_b[18][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, n) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, n) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        cout << l << ' ' << r  << ' ' << id << endl;
        exit(0);
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            //cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
int Log[MAXN];
int table_a[20][MAXN];
int table_b[20][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

int get(int id) {
    int l = id - 1, r = n + 1;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(id, mid) >= getMin_for_b(id, mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    int LEFT = 0;
    if (getMax_for_a(id, r) == getMin_for_b(id, r)) {
        LEFT = r;
    } else {
        return 0;
    }
    l = id, r = n + 1;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(id, mid) > getMin_for_b(id, mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(id, l) == getMin_for_b(id, l)) {
        return l - LEFT + 1;
    } else {
        return 0;
    }
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    ///...///
    for (int i = 1; i <= n; i++) {
        ans += get(i);
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[18][MAXN];
int table_b[18][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, n) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, n) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            //cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[18][MAXN];
int table_b[18][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, n) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, n) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        cout << l << ' ' << r << endl;
        exit(0);
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            //cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second

const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 777;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' | ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

int n;

int a[MAXN];
int b[MAXN];
pair <int, int> ans_for_a[MAXN];
pair <int, int> ans_for_b[MAXN];
int Log[MAXN];
int table_a[18][MAXN];
int table_b[18][MAXN];

int getMax_for_a(int l, int r) {
    int curlog = Log[r - l + 1];
    return max(table_a[curlog][l], table_a[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_a() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_a[i][j] = a[j];
                continue;
            }
            table_a[i][j] = max(table_a[i - 1][j], table_a[i - 1][j + len / 2]);
        }
    }
}

int getMin_for_b(int l, int r) {
    int curlog = Log[r - l + 1];
    return min(table_b[curlog][l], table_b[curlog][r - (1 << curlog) + 1]);
}

void build_sparse_for_b() {
    for (int i = 0; i <= Log[n]; i++) {
        int len = (1 << i);
        for (int j = 1; j <= n; j++) {
            if (i == 0) {
                table_b[i][j] = b[j];
                continue;
            }
            table_b[i][j] = min(table_b[i - 1][j], table_b[i - 1][j + len / 2]);
        }
    }
}

pair <int, int> get_ans_for_a(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(mid, id) == a[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMax_for_a(l, id) == a[id]) {
        tmp.first = l;
    } else if (getMax_for_a(r, id) == a[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMax_for_a(id, mid) == a[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMax_for_a(id, r) == a[id]) {
        tmp.second = r;
    } else if (getMax_for_a(id, l) == a[id]) {
        tmp.second = l;
    } else {
        assert(0);
    }
    return tmp;
}

pair <int, int> get_ans_for_b(int id) {
    pair <int, int> tmp;
    int l = 1, r = id;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(mid, id) == b[id]) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if (getMin_for_b(l, id) == b[id]) {
        tmp.first = l;
    } else if (getMin_for_b(r, id) == b[id]) {
        tmp.first = r;
    } else {
        assert(0);
    }
    l = id, r = n;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if (getMin_for_b(id, mid) == b[id]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (getMin_for_b(id, r) == b[id]) {
        tmp.second = r;
    } else if (getMin_for_b(id, l) == b[id]) {
        tmp.second = l;
    } else {
        cout << l << ' ' << r  << ' ' << id << endl;
        exit(0);
        assert(0);
    }
    return tmp;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    n = readInt();
    Log[1] = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = readInt();
        if (i > 1)
            Log[i] = Log[i / 2] + 1;
    }
    build_sparse_for_a();
    for (int i = 1; i <= n; i++) {
        b[i] = readInt();
    }
    int ans = 0;
    build_sparse_for_b();
    for (int i = 1; i <= n; i++) {
        ans_for_a[i] = get_ans_for_a(i);
        ans_for_b[i] = get_ans_for_b(i);
        if (a[i] == b[i]) {
            int L = max(ans_for_a[i].first, ans_for_b[i].first);
            L = i - L;
            L ++;
            int R = min(ans_for_a[i].second, ans_for_b[i].second);
            R -= i;
            R ++;
            //cout << L << ' ' << R <<  ' ' <<  i << endl;
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

constexpr auto LLINF = (1LL<<60);
constexpr auto INF = (1LL<<30);
constexpr auto MOD = 1e9+7;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';REP(i,a.size()){out<<a[i];if(i!=a.size()-1)out<<',';}out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

template <typename S>
class sparseTable {
public:
  using T = typename S::T;
  int n;
  vector<int> log2;
  vector<vector<T>> t;

  sparseTable(int nn) {
    n = nn;
    log2.assign(n+1, 0);
    for(int i=2; i<=n; ++i) log2[i] = log2[i >> 1] + 1;
    t = vector<vector<T>>(log2[n]+1, vector<T>(n));
  }
  void init(vector<T> v) {
    for(int i=0; i<n; ++i) t[0][i] = v[i];
    for(int j=1; j<=log2[n]; ++j) {
      int w = 1LL<<(j-1);
      for (int i = 0; i+(w<<1) <= n; ++i) {
        t[j][i] = S::op(t[j-1][i], t[j-1][i+w]);
      }
    }
  }
  // [l, r]
  T query(int l, int r) {
    int j = log2[r - l];
    return S::op(t[j][l], t[j][r-(1 << j)+1]);
  }
};

// 集合T、結合則・可換・冪等律が成り立つ二項演算op
struct minnimum {
  using T = int;
  static T op(const T& a, const T& b) { return min(a, b); }
};
struct maximum {
  using T = int;
  static T op(const T& a, const T& b) { return max(a, b); }
};
struct gcd {
  using T = int;
  static T op(const T& a, const T& b) { return __gcd(a, b); }
};

signed main(void)
{
  int n;
  cin >> n;
  VI a(n), b(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];

  sparseTable<maximum> sp1(n);  sp1.init(a);
  sparseTable<minnimum> sp2(n); sp2.init(b);

  int ret = 0;
  // [l, r]
  REP(l, n) {
    if(a[l] > b[l]) continue;

    // (lb, ub]
    int lb = l-1, ub = n-1;
    while(ub-lb>1) {
      int mid = (lb+ub)/2;
      if(sp1.query(l, mid) - sp2.query(l, mid) >= 0) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    int rmin = ub;
    if(sp1.query(l, rmin) != sp2.query(l, rmin)) continue;

    // [lb, ub)
    lb = l, ub = n;
    while(ub-lb>1) {
      int mid = (lb+ub)/2;
      if(sp1.query(l, mid) - sp2.query(l, mid) <= 0) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    int rmax = lb;

    if(rmax >= rmin) {
      ret += rmax - rmin + 1;
    }
  }

  cout << ret << endl;
  return 0;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

constexpr auto LLINF = (1LL<<60);
constexpr auto INF = (1LL<<30);
constexpr auto MOD = 1e9+7;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';REP(i,a.size()){out<<a[i];if(i!=a.size()-1)out<<',';}out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

template <typename S>
class sparseTable {
public:
  using T = typename S::T;
  int n;
  vector<int> log2;
  vector<vector<T>> t;

  sparseTable(int nn) {
    n = nn;
    log2.assign(n+1, 0);
    for(int i=2; i<=n; ++i) log2[i] = log2[i >> 1] + 1;
    t = vector<vector<T>>(log2[n]+1, vector<T>(n));
  }
  void init(vector<T> v) {
    for(int i=0; i<n; ++i) t[0][i] = v[i];
    for(int j=1; j<=log2[n]; ++j) {
      int w = 1LL<<(j-1);
      for (int i = 0; i+(w<<1) <= n; ++i) {
        t[j][i] = S::op(t[j-1][i], t[j-1][i+w]);
      }
    }
  }
  // [l, r)
  T query(int l, int r) {
    int j = log2[r - l];
    return S::op(t[j][l], t[j][r - (1 << j)]);
  }
};

// 集合T、結合則・可換・冪等律が成り立つ二項演算op
struct minnimum {
  using T = int;
  static T op(const T& a, const T& b) { return min(a, b); }
};
struct maximum {
  using T = int;
  static T op(const T& a, const T& b) { return max(a, b); }
};

signed main(void)
{
  int n;
  cin >> n;
  VI a(n), b(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];

  sparseTable<maximum> sp1(n);
  sp1.init(a);
  sparseTable<minnimum> sp2(n);
  sp2.init(b);

  // cout << sp1.t << endl;
  // cout << sp2.t << endl;
  //
  // REP(i, n) {
  //   FOR(j, i+1, n+1) {
  //     cout << sp1.query(i, j) - sp2.query(i, j) << " ";
  //   }
  //   cout << endl;
  // }

  // REP(i, n) {
  //   FOR(j, i+1, n+1) {
  //     cout << sp2.query(i, j) << " ";
  //   }
  //   cout << endl;
  // }

  int ret = 0;
  // [l, r]
  REP(l, n) {
    if(a[l]-b[l] > 0) continue;
    // (lb, ub]
    int lb = l-1, ub = n-1;
    while(ub-lb>1) {
      int mid = (lb+ub)/2;
      // cout << lb << " " << mid << " " << ub << endl;
      if(sp1.query(l, mid+1) - sp2.query(l, mid+1) >= 0) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    int rmin = ub;
    // [lb, ub)
    lb = l, ub = n;
    while(ub-lb>1) {
      int mid = (lb+ub)/2;
      // cout << lb << " " << mid << " " << ub << endl;
      if(sp1.query(l, mid+1) - sp2.query(l, mid+1) <= 0) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    int rmax = lb;
    // cout << l << ":" << rmin << ":" << rmax << endl;
    if(rmax >= rmin) {
      ret += rmax - rmin + 1;
    }
  }

  cout << ret << endl;
  return 0;
}


//33新方法
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	while(cin>>s)
	{
		int len=s.size();
		int temp=0;
		if(s[0]==s[len-1])
		{
			int ans=1;
			for(int i=1;i<len;i++)
			{
				//printf("s[i]:%c,s[i-1]:%c\n",s[i],s[i-1]);
				if(s[i]!=s[i-1])
				{
					ans++;
				}
				else
				{
					temp=max(temp,ans);
					ans=1;
				}
			}
			temp=max(temp,ans);
		}
		else
		{
			int time=0;
			int ans=1;
			for(int i=1;i<=len;i++)
			{
				if(i==len)
				{
					time++;
					i=0;
					if(s[0]!=s[len-1])
					{
						ans++;
					}
					else
					{
						temp=max(temp,ans);
						ans=1;
					}
					continue;
				}
				if(time==2) break;
				if(s[i]!=s[i-1])
				{
					ans++;
				}
				else
				{
					temp=max(temp,ans);
					ans=1;
				}
			}
			temp=max(temp,ans);
		}
		printf("%d\n",temp);
	}
 }


#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define For(I,X,Y) for(LL I=(X);I<=(Y);I++)
LL max(LL X,LL Y){return X>Y?X:Y;}
LL min(LL X,LL Y){return X>Y?Y:X;}
char CH[100001<<1];
const LL Max=1e5+5;
LL Len,Cur,Ans;
int main(){
    scanf("%s",CH+1);
	Len=strlen(CH+1);
    For(I,1,Len) CH[I+Len]=CH[I];
    For(I,1,2*Len)
		if(CH[I]!=CH[I-1]) Ans=max(Ans,++Cur);
		else Cur=1;
	printf("%d",min(Ans,Len));
	return 0;
}


#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define mod 10003
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<30;
const int maxn = 1000003;
const double eps = 1e-8;
int t,n,m,q;
int sum[maxn];
int p[maxn],l,r,x,y;
char s[maxn];
LL min(LL x,LL y){return x>y?y:x;}
LL max(LL x,LL y){return x>y?x:y;}

int main()
{
    while(~scanf("%s",s+1))
    {
        LL ans=0,cnt=0;
        int n=strlen(s+1);
        for(int i=1;i<=n;i++)
        {
            s[i+n]=s[i];
        }
        for(int i=1;i<=2*n;i++)
        {
            if(s[i]!=s[i-1])
                ans=max(ans,++cnt);
            else cnt=1;
        }
        cout<<min(ans,n)<<endl;
    }
    return 0;
}
/*
设原串为s1,s2,...sn，我们将原串拉长一倍得到：S=s1,..sn...s2n
很显然的一个事实：无论将原串如何合法处理，得到的新串始终是S的一个子串。

所以我们此时只需要从头到尾去扫一遍统计前后相等与不相等的最长长度即可。
*/


#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string a;
int main()
{
	cin>>a;
	a+=a;
	int ans=1,cnt=1;
	for(int i=1;i<a.size();i++)
	{
		if(a[i]!=a[i-1])
		{
			cnt++;
			ans=max(ans,cnt);
		}
		else
		{
			cnt=1;
		}
	}
	ans=min(ans,int(a.size()/2));
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
const int maxn=100000+9;
const int inf=0x3f3f3f3f;
inline int read(){
	int a=1,b=0;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-') a=-1;c=getchar();}
	while(c>='0'&&c<='9'){b=(b<<3)+(b<<1)+c-'0';c=getchar();}
	return a*b;
}
inline void print(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) print(x/10);
	putchar(x%10+'0');
}
int main(){
	string s;
	cin>>s;
	s+=s;
	int ans=0,now=1;
	for(int i=1,k=s.size();i<k;++i)
	{
		if(s[i]==s[i-1]) now=1;
		else now++,ans=max(ans,now);
	}
	print(min(ans,(int)s.size()/2));
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<iostream>
using namespace std;
char s[600000];
int n,a[600000];
int ans=0,ans1=0,hh=0,hh1=0;
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;++i){
		a[i]=(s[i]=='b'?1:0);
	}
	hh=1;
	for(int i=1;i<=n;i++){
	    if(a[i]!=a[i+1]) hh++;
	    else{
	        hh1=max(hh1,hh);
	        hh=1;
	    }
	}
	ans=1;ans1=1;
	for(int i=1;i<=n;++i){
		if(a[i]!=a[i+1]){
			ans++;
		}
		else break;
	}
	for(int i=n;i>0;i--){
		if(a[i]!=a[i-1]){
			ans1++;
		}
		else break;
	}
	if(a[1]!=a[n]){
		if(ans+ans1<n){
			printf("%d",max(hh1,ans+ans1));
		}
		else printf("%d",n);
		return 0;
	}
	else{
	    if(ans<n&&ans1<n){
	        printf("%d",max(ans,max(ans1,hh1)));
	    }
	    else printf("%d",n);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define N 1234567
#define ll long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define pl pair<ll,ll>
#define vl vector<ll>
const ll mod = 1e9 + 7, inf = 1e18;
ll modpow(ll n, ll p, ll m) { ll ret = 1; while(p > 0) { if(p & 1) ret = (ret * n) % m; p >>= 1; n = (n * n) % m; } return ret; }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
	fast;
	int t = 1;
	while(t--) {
	string s;
	cin >> s;
	int n = s.length();
	int ans = 1, i = 0, ans2 = 1;
	int tempans = -1, cnt = 1;
	for(int i = 0; i < n - 1; ++i) {
		if(s[i] != s[i + 1]) cnt++;
		else {
			tempans = (tempans == -1) ? cnt: max(tempans, cnt);
			cnt = 1;
		}
	}
	tempans = (tempans == -1) ? cnt: max(tempans, cnt);
	if(tempans == 1) tempans = 0;
	if(tempans == 0) { cout << 0 << endl; continue; }

	while(ans < n) {
		if(s[i] == s[(i + 1) % n]) {
			break;
		}
		++ans;
		++i;
	}
	if(ans == 1) ans = 0;
	if(ans >= n) {
		cout << ans << endl;
		continue;
	}
	int j = n - 1;
	while(ans2 < n) {
		if(s[j] == s[(j - 1 + n) % n]) {
			break;
		}
		++ans2;
		--j;
	}
	if(ans2 == 1) ans2 = 0;
	// cout << ans << ' ' << ans2 << ' ' << i << ' ' << j << ' ' << tempans << endl;
	if(s[n - 1] == s[0]) {
		cout << max(ans, max(ans2, tempans)) << endl;
		continue;
	}
	cout << max(min(ans + ans2, n), tempans) << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define N 1234567
#define ll long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define pl pair<ll,ll>
#define vl vector<ll>
const ll mod = 1e9 + 7, inf = 1e18;
ll modpow(ll n, ll p, ll m) { ll ret = 1; while(p > 0) { if(p & 1) ret = (ret * n) % m; p >>= 1; n = (n * n) % m; } return ret; }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
	fast;
	int t = 1;
	while(t--) {
	string s;
	cin >> s;
	int n = s.length();
	int ans = 1, i = 0, ans2 = 1;
	int tempans = -1, cnt = 1;
	for(int i = 0; i < n - 1; ++i) {
		if(s[i] != s[i + 1]) cnt++;
		else {
			tempans = (tempans == -1) ? cnt: max(tempans, cnt);
			cnt = 1;
		}
	}
	tempans = (tempans == -1) ? cnt: max(tempans, cnt);
	if(tempans == 0) { cout << 0 << endl; continue; }

	while(ans < n) {
		if(s[i] == s[(i + 1) % n]) {
			break;
		}
		++ans;
		++i;
	}
	// if(ans == 1) ans = 0;
	if(ans >= n) {
		cout << ans << endl;
		continue;
	}
	int j = n - 1;
	while(ans2 < n) {
		if(s[j] == s[(j - 1 + n) % n]) {
			break;
		}
		++ans2;
		--j;
	}
	// if(ans2 == 1) ans2 = 0;
	// cout << ans << ' ' << ans2 << ' ' << i << ' ' << j << ' ' << tempans << endl;
	if(s[n - 1] == s[0]) {
		cout << max(ans, max(ans2, tempans)) << endl;
		continue;
	}
	cout << max(min(ans + ans2, n), tempans) << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define N 1234567
#define ll long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define pl pair<ll,ll>
#define vl vector<ll>
const ll mod = 1e9 + 7, inf = 1e18;
ll modpow(ll n, ll p, ll m) { ll ret = 1; while(p > 0) { if(p & 1) ret = (ret * n) % m; p >>= 1; n = (n * n) % m; } return ret; }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
	fast;
	int t = 14;
	while(t--) {
	string s;
	cin >> s;
	int n = s.length();
	int ans = 1, i = 0, ans2 = 1;
	int tempans = -1, cnt = 1;
	for(int i = 0; i < n - 1; ++i) {
		if(s[i] != s[i + 1]) cnt++;
		else {
			tempans = (tempans == -1) ? cnt: max(tempans, cnt);
			cnt = 1;
		}
	}
	tempans = (tempans == -1) ? cnt: max(tempans, cnt);
	if(tempans == 0) { cout << 0 << endl; continue; }

	while(ans < n) {
		if(s[i] == s[(i + 1) % n]) {
			break;
		}
		++ans;
		++i;
	}
	if(ans == 1) ans = 0;
	if(ans >= n) {
		cout << ans << endl;
		continue;
	}
	int j = n - 1;
	while(ans2 < n) {
		if(s[j] == s[(j - 1 + n) % n]) {
			break;
		}
		++ans2;
		--j;
	}
	if(ans2 == 1) ans2 = 0;
	// cout << ans << ' ' << ans2 << ' ' << i << ' ' << j << ' ' << tempans << endl;
	if(s[n - 1] == s[0]) {
		cout << max(ans, max(ans2, tempans)) << endl;
		continue;
	}
	cout << max(min(ans + ans2, n), tempans) << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define N 1234567
#define ll long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define pl pair<ll,ll>
#define vl vector<ll>
const ll mod = 1e9 + 7, inf = 1e18;
ll modpow(ll n, ll p, ll m) { ll ret = 1; while(p > 0) { if(p & 1) ret = (ret * n) % m; p >>= 1; n = (n * n) % m; } return ret; }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
	fast;
	int t = 1;
	while(t--) {
	string s;
	cin >> s;
	int n = s.length();
	int ans = 1, i = 0, ans2 = 1;
	int tempans = -1, cnt = 1;
	for(int i = 0; i < n - 1; ++i) {
		if(s[i] != s[i + 1]) cnt++;
		else {
			tempans = (tempans == -1) ? cnt: max(tempans, cnt);
			cnt = 1;
		}
	}
	tempans = (tempans == -1) ? cnt: max(tempans, cnt);
	if(tempans == 0) { cout << 0 << endl; continue; }

	while(ans < n) {
		if(s[i] == s[(i + 1) % n]) {
			break;
		}
		++ans;
		++i;
	}
	if(ans == 1) ans = 0;
	if(ans >= n) {
		cout << ans << endl;
		continue;
	}
	int j = n - 1;
	while(ans2 < n) {
		if(s[j] == s[(j - 1 + n) % n]) {
			break;
		}
		++ans2;
		--j;
	}
	if(ans2 == 1) ans2 = 0;
	// cout << ans << ' ' << ans2 << ' ' << i << ' ' << j << ' ' << tempans << endl;
	if(s[n - 1] == s[0]) {
		cout << max(ans, max(ans2, tempans)) << endl;
		continue;
	}
	cout << max(min(ans + ans2, n), tempans) << endl;
	}
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int zb[300010];
int main()
{
	int jsq;
	string xx;
	cin>>xx;
	jsq=xx.size();
	for (int i=0;i<jsq;i++)
	{
		if (xx[i]=='b') zb[i+1]=1;
		else zb[i+1]=0;
	}
	for (int i=1;i<=jsq;i++)
	{
		zb[i+jsq]=zb[i];
		zb[i+jsq*2]=zb[i];
	}
	int ans=1,zz=1;
	for (int i=jsq-1;i<=2*jsq+1;i++)
	{
		if (zb[i]!=zb[i-1])
		{
			zz++;
		}
		else
		{
			ans=max(zz, ans);
			zz=1;
		}
	}
	ans=min(ans, jsq);
	cout<<ans;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int zb[300010];
int main()
{
	int jsq;
	string xx;
	cin>>xx;
	jsq=xx.size();
	for (int i=0;i<jsq;i++)
	{
		if (xx[i]=='b') zb[i+1]=1;
		else zb[i+1]=0;
	}
	for (int i=1;i<=jsq;i++)
	{
		zb[i+jsq]=zb[i];
		zb[i+jsq*2]=zb[i];
	}
	int ans=1,zz=1;
	for (int i=2;i<=2*jsq+1;i++)
	{
		if (zb[i]!=zb[i-1])
		{
			zz++;
			ans=max(zz, ans);
		}
		else
		{
			ans=max(zz, ans);
			zz=1;
		}
	}
	ans=min(ans, jsq);
	cout<<ans;
	return 0;
}


/*By DennyQi 2018*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define  r  read()
using namespace std;
typedef long long ll;
const int MAXN = 10010;
const int MAXM = 20010;
const int INF = 1061109567;
inline int Max(const int a, const int b){ return (a > b) ? a : b; }
inline int Min(const int a, const int b){ return (a < b) ? a : b; }
inline int read(){
    int x = 0; int w = 1; register char c = getchar();
    for(; c ^ '-' && (c < '0' || c > '9'); c = getchar());
    if(c == '-') w = -1, c = getchar();
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x<<3) + (x<<1) + c - '0'; return x * w;
}
int N,ans;
char s[200010];
int f[200010];
int main(){
	scanf("%s", s);
	N = strlen(s);
	for(int i = N; i < 2 * N; ++i) s[i] = s[i-N];
	for(int i = 0; i < 2 * N; ++i){
		if(i - 1 >= 0){
			if(s[i] != s[i-1]){
				f[i] = f[i-1] + 1;
			}
			else{
				f[i] = 1;
			}
		}
		else{
			f[i] = 1;
		}
		if(f[i] <= N){
			ans = Max(ans, f[i]);
		}
	}
	printf("%d", ans);
	return 0;
}


#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string a;
int main()
{
	cin>>a;
	a+=a;
	int ans=1,cnt=1;
	for(int i=0;i<a.size();i++)
	{
		if(a[i]!=a[i-1])
		{
			cnt++;
			ans=max(ans,cnt);
		}
		else
		{
			cnt=1;
		}
	}
	ans=min(ans,int(a.size()/2));
	printf("%d",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=101000;
int n,m,data[2*N],data2[2*N];
char str[2*N],str2[2*N];
int main(){
	int i,j;
	scanf("%s",str+1);
	int n=strlen(str+1);
	memset(data,0,sizeof(data));memset(data2,0,sizeof(data2));
	for (i=1;i<=n;i++) {
		if (str[i]=='w') data[i]=1;
		data[i+n]=data[i];data2[n-i+1]=data[i];
		data2[2*n-i+1]=data[i];
	}
	int ans1=0,ans2=0,ans=0;
	for (i=2;i<=2*n;i++){
		if (data[i]^data[i-1]) ans1++; else ans1=1;
		if (data2[i]^data2[i-1]) ans2++; else ans2=1;
		ans=max(ans,max(ans1,ans2));
	}
	ans=min(ans,n);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int lz(string s){
    int mx=1;
    int i=0;
    int l=s.length();
    int a[l];
    while(i<l-1){
        int temp=1;
        while(i<l-1&&s[i]!=s[i+1]){
            temp++;
            i++;
            //cout<<s[i]<<" "<<temp<<" ";
        }
        //cout<<endl;
        mx=max(mx,temp);
        i++;
    }
    return mx;
}
void solve(){
    string s;
    cin>>s;

    int ans=lz(s);

    int fans=ans;
    //cout<<fans<<endl;
    string t=s;
    t=t+s;
    int l=s.length();
    int i=0;
    while(i<l-1){
        if(t[i]!=t[i+1])ans--;
        if(t[i+l]!=t[i+l-1])ans++;
        fans=max(ans,fans);
        i++;
    }
    cout<<fans;
    return;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    //cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int lz(string s){
    int mx=1;
    int i=0;
    int l=s.length();
    int a[l];
    while(i<l-1){
        int temp=1;
        while(i<l-1&&temp<l/2&&s[i]!=s[i+1]){
            temp++;
            i++;

        }
        mx=max(mx,temp);
        i++;
    }
    return mx;
}
void solve(){
    string s;
    cin>>s;
    string t=s;
    t=t+s;

    int ans=lz(t);
    cout<<ans;
    return;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    //cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int lz(string s){
    int mx=1;
    int i=0;
    int l=s.length();
    int a[l];
    while(i<l-1){
        int temp=1;
        while(i<l-1&&s[i]!=s[i+1]){
            temp++;
            i++;
            //cout<<s[i]<<" "<<temp<<" ";
        }
        //cout<<endl;
        mx=max(mx,temp);
        i++;
    }
    return mx;
}
void solve(){
   string s;
   cin>>s;
   int l=s.length();

   if(s[l-1]==s[0]){
        cout<<lz(s);
        return;
    }
    int i=0,count1=0,count2=0;
    int k=l;
    while(i<l-1&&s[i]!=s[i+1]){
    i++;
    count1++;}
    while(k>0&&s[k]!=s[k-1]){
        k--;
        count2++;}

    if(i>=l-1){
    //cout<<"yaay";
        cout<<lz(s);
        return;
    }
    cout<<i+1+(l-1-k)+1;
    return;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
    //cin>>t;
	while(t--){
		solve();
	}
	return 0;
}