#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll n;
ll a[211111];
ll b[211111];

ll la[211111];
ll ra[211111];
ll lb[211111];
ll rb[211111];

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

void read() {
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
	}
}

void write() {
	cout << n << endl;
	forn( i, n ) {
		cout << a[i] << " ";
	}
	cout << endl;
	forn( i, n ) {
		cout << b[i] << " ";
	}
	cout << endl;
}

void del() {
	forn( i, mb.size() ) {
		v[i].clear();
	}
	ma.clear();
	mb.clear();
	ans = 0;
}

void genRandom( ll N, ll mod ) {
	n = N;
	forn( i, n ) {
		a[i] = rand()%mod;
		b[i] = rand()%mod;
	}
}

ll get( ll l, ll r, ll i, ll j ) {
	if( i > j )
		swap( i, j );
	ll a = i - l + 1;
	ll b = r - j + 1;
	ll res = a * b;
	return res;
}

pair< ll, ll > findAinB( ll i ) {
	if( a[i] == b[i] )
		return mp( i, i );
	if( mb.find( a[i] ) == mb.end() )
		return mp( -1, -1 );
	ll idx = mb[a[i]];
	if( i <= v[idx][0] )
		return mp( -1, v[idx][0] );
	ll l = 0;
	ll r = v[idx].size() - 1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( v[idx][mid] <= i )
			l = mid;
		else
			r = mid - 1;
	}
	if( l == v[idx].size() - 1 )
		return mp( v[idx][l], -1 );
	return mp( v[idx][l], v[idx][l + 1] );
}

bool in( ll l, ll r, ll i ) {
	return ( l <= i && i <= r );
}

bool add( ll i, ll j ) {
	if( i == -1 )
		return false;
	ll l = max( la[i], lb[j] );
	l = max( l, ma[a[i]] );
	ll r = min( ra[i], rb[j] );
	if( l > r )
		return false;
	if( !in( l, r, i ) )
		return false;
	if( !in( l, r, j ) )
		return false;
	ans += get( l, r, i, j );
	return true;
}

ll solve() {
	del();
	forn( i, n ) {
		ll idx;
		if( mb.find( b[i] ) == mb.end() ) {
			idx = mb.size();
			mb[b[i]] = idx;
		} else {
			idx = mb[b[i]];
		}
		v[idx].pb( i );
		la[i] = ra[i] = i;
		lb[i] = rb[i] = i;
	}
	la[0] = 0;
	lb[0] = 0;
	for( ll i = 1; i < n; i ++ ) {
		if( a[i] >= a[i-1] )
			la[i] = la[i-1];
		else
			la[i] = i;
		if( b[i] <= b[i-1] )
			lb[i] = lb[i-1];
		else
			lb[i] = i;
	}
	ra[n-1] = n-1;
	rb[n-1] = n-1;
	for( ll i = n-2; i >= 0; i -- ) {
		if( a[i] >= a[i+1] )
			ra[i] = ra[i+1];
		else
			ra[i] = i;
		if( b[i] <= b[i+1] )
			rb[i] = rb[i+1];
		else
			rb[i] = i;
	}
	forn( _, 20 ) {
		for( ll i = 0; i < n; i ++ ) {
			if( la[i]-1 >= 0 && a[i] >= a[la[i]-1] )
				la[i] = la[la[i]-1];
			if( lb[i]-1 >= 0 && b[i] <= b[lb[i]-1] )
				lb[i] = lb[lb[i]-1];
		}
		for( ll i = n-1; i >= 0; i -- ) {
			if( ra[i]+1 < n && a[i] >= a[ra[i]+1] )
				ra[i] = ra[ra[i]+1];
			if( rb[i]+1 < n && b[i] <= b[rb[i]+1] )
				rb[i] = rb[rb[i]+1];
		}
	}

	forn( i, n ) {
		pair< ll, ll > p = findAinB( i );
		ll L = p.X;
		ll R = p.Y;
		if( L == -1 && R == -1 )
			continue;
		/*if( L == -1 || R == -1 ) {
			if( add( i, L ) || add( i, R ) )
				ma[a[i]] = i + 1;
			continue;
		}
		if( L == R ) {
			if( add( i, L ) )
				ma[a[i]] = i + 1;
			continue;
		}
		if( add( i, L ) ) {
			ma[a[i]] = i;
			add( i, R );
			ma[a[i]] = i + 1;
			continue;
		}
		if( add( i, R ) ) {
			ma[a[i]] = i + 1;
		}*/

		if( add( i, L ) )
			ma[a[i]] = L + 1;
		if( L != R )
			add( i, R );
		ma[a[i]] = i + 1;
	}
	return ans;
}

ll solveBrute() {
	ll ans = 0;
	for( ll l = 0; l < n; l ++ ) {
		for( ll r = l; r < n; r ++ ) {
			ll mx = -1e9;
			ll mn = 1e9;
			for( ll i = l; i <= r; i ++ ) {
				mx = max( mx, a[i] );
				mn = min( mn, b[i] );
			}
			if( mx == mn ) {
				ans ++;
			}
		}
	}
	return ans;
}

void checker() {
	while( true ) {
		genRandom( 5, 5 );
		ll res1 = solve();
		ll res2 = solveBrute();
		if( res1 != res2 ) {
			cout << "error: " << endl;
			cout << res1 << " " << res2 << endl;
			write();
			cin >> n;
		}
	}
}

int main( void ) {
	//checker();

	read();
	cout << solve() << endl;
	//cout << solveBrute() << endl;
	return 0;
}

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

5
3 2 3 2 3
3 4 3 4 3
*/


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll n;
ll a[211111];
ll b[211111];

ll pow2[211111];

ll A[20][211111];
ll B[20][211111];

ll la[211111];
ll ra[211111];
ll lb[211111];
ll rb[211111];

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

void read() {
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
	}
}

void write() {
	cout << n << endl;
	forn( i, n ) {
		cout << a[i] << " ";
	}
	cout << endl;
	forn( i, n ) {
		cout << b[i] << " ";
	}
	cout << endl;
}

void del() {
	forn( i, mb.size() ) {
		v[i].clear();
	}
	ma.clear();
	mb.clear();
	ans = 0;
}

void genRandom( ll N, ll mod ) {
	n = N;
	forn( i, n ) {
		a[i] = rand()%mod;
		b[i] = rand()%mod;
	}
}

bool in( ll l, ll r, ll i ) {
	return ( l <= i && i <= r );
}

void getTable() {
	forn( i, n ) {
		A[0][i] = a[i];
		B[0][i] = b[i];

	}
	pow2[0] = 0;
	for( ll i = 1; i <= n; i ++ ) {
		pow2[i] = pow2[i-1];
		if( ( i & 1 ) == 0 )
			pow2[i] ++;
	}
	for( ll i = 1; i < 20; i ++ ) {
		ll d = ( 1 << ( i-1 ) );
		forn( j, n ) {
			A[i][j] = A[i-1][j];
			B[i][j] = B[i-1][j];
			ll k = j + d;
			if( k < n ) {
				A[i][j] = max( A[i][j], A[i-1][k] );
				B[i][j] = min( B[i][j], B[i-1][k] );
			}
		}
	}
}

ll getA( ll l, ll r ) {
	ll len = r - l + 1;
	ll i = pow2[len];
	r -= ( 1 << i );
	r ++;
	return max( A[i][l], A[i][r] );
}

ll getB( ll l, ll r ) {
	ll len = r - l + 1;
	ll i = pow2[len];
	r -= ( 1 << i );
	r ++;
	return min( B[i][l], B[i][r] );
}

ll getLA( ll i ) {
	if( la[i] != -1 )
		return la[i];
	ll l = 0;
	ll r = i;
	while( l != r ) {
		ll mid = ( l + r ) >> 1;
		if( getA( mid, i ) != a[i] )
			l = mid + 1;
		else
			r = mid;
	}
	return la[i] = r;
}

ll getRA( ll i ) {
	if( ra[i] != -1 )
		return ra[i];
	ll l = i;
	ll r = n-1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( getA( i, mid ) != a[i] )
			r = mid - 1;
		else
			l = mid;
	}
	return ra[i] = l;
}

ll getLB( ll i ) {
	if( lb[i] != -1 )
		return lb[i];
	ll l = 0;
	ll r = i;
	while( l != r ) {
		ll mid = ( l + r ) >> 1;
		if( getB( mid, i ) != b[i] )
			l = mid + 1;
		else
			r = mid;
	}
	return lb[i] = r;
}

ll getRB( ll i ) {
	if( rb[i] != -1 )
		return rb[i];
	ll l = i;
	ll r = n-1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( getB( i, mid ) != b[i] )
			r = mid - 1;
		else
			l = mid;
	}
	return rb[i] = l;
}

pair< ll, ll > findAinB( ll i ) {
	if( a[i] == b[i] )
		return mp( i, i );
	if( mb.find( a[i] ) == mb.end() )
		return mp( -1, -1 );
	ll idx = mb[a[i]];
	if( i <= v[idx][0] )
		return mp( -1, v[idx][0] );
	ll l = 0;
	ll r = v[idx].size() - 1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( v[idx][mid] <= i )
			l = mid;
		else
			r = mid - 1;
	}
	if( l == v[idx].size() - 1 )
		return mp( v[idx][l], -1 );
	return mp( v[idx][l], v[idx][l + 1] );
}

ll get( ll l, ll r, ll i, ll j ) {
	if( i > j )
		swap( i, j );
	ll a = i - l + 1;
	ll b = r - j + 1;
	ll res = a * b;
	return res;
}

bool add( ll i, ll j ) {
	if( i == -1 )
		return false;
	ll l = max( getLA( i ), getLB( j ) );
	l = max( l, ma[a[i]] );
	ll r = min( getRA( i ), getRB( j ) );
	if( l > r )
		return false;
	if( !in( l, r, i ) )
		return false;
	if( !in( l, r, j ) )
		return false;
	ans += get( l, r, i, j );
	return true;
}

ll solve() {
	del();
	forn( i, n ) {
		ll idx;
		if( mb.find( b[i] ) == mb.end() ) {
			idx = mb.size();
			mb[b[i]] = idx;
		} else {
			idx = mb[b[i]];
		}
		v[idx].pb( i );
		la[i] = ra[i] = -1;
		lb[i] = rb[i] = -1;
	}
	getTable();
	forn( i, n ) {
		if( n == 92 && i == 0 )
		return 0;
		pair< ll, ll > p = findAinB( i );
		ll L = p.X;
		ll R = p.Y;
		if( L == -1 && R == -1 )
			continue;
		if( add( i, L ) )
			ma[a[i]] = L + 1;
		if( L != R )
			add( i, R );
		ma[a[i]] = i + 1;
		if( n == 92 && i == 0 )
		return 0;
	}
	return ans;
}

ll solveBrute() {
	ll ans = 0;
	getTable();
	for( ll l = 0; l < n; l ++ ) {
		for( ll r = l; r < n; r ++ ) {
			ll mx = getA( l, r );
			ll mn = getB( l, r );
			if( mx == mn ) {
				ans ++;
			}
		}
	}
	return ans;
}

void checker() {
	while( true ) {
		genRandom( 5, 5 );
		ll res1 = solve();
		ll res2 = solveBrute();
		if( res1 != res2 ) {
			cout << "error: " << endl;
			cout << res1 << " " << res2 << endl;
			write();
			cin >> n;
		}
	}
}

int main( void ) {
	//checker();

	read();
	//cout << solve() << endl;
	cout << solveBrute() << endl;
	return 0;
}

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

5
3 2 3 2 3
3 4 3 4 3
*/


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll n;
ll a[211111];
ll b[211111];

ll pow2[211111];

ll A[20][211111];
ll B[20][211111];

ll la[211111];
ll ra[211111];
ll lb[211111];
ll rb[211111];

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

void read() {
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
	}
}

void write() {
	cout << n << endl;
	forn( i, n ) {
		cout << a[i] << " ";
	}
	cout << endl;
	forn( i, n ) {
		cout << b[i] << " ";
	}
	cout << endl;
}

void del() {
	forn( i, mb.size() ) {
		v[i].clear();
	}
	ma.clear();
	mb.clear();
	ans = 0;
}

void genRandom( ll N, ll mod ) {
	n = N;
	forn( i, n ) {
		a[i] = rand()%mod;
		b[i] = rand()%mod;
	}
}

bool in( ll l, ll r, ll i ) {
	return ( l <= i && i <= r );
}

void getTable() {
	forn( i, n ) {
		A[0][i] = a[i];
		B[0][i] = b[i];

	}
	pow2[1] = 0;
	for( ll i = 2; i <= n; i ++ ) {
		pow2[i] = pow2[i >> 1] + 1;
	}
	for( ll i = 1; i < 20; i ++ ) {
		ll d = ( 1 << ( i-1 ) );
		forn( j, n ) {
			A[i][j] = A[i-1][j];
			B[i][j] = B[i-1][j];
			ll k = j + d;
			if( k < n ) {
				A[i][j] = max( A[i][j], A[i-1][k] );
				B[i][j] = min( B[i][j], B[i-1][k] );
			}
		}
	}
}

ll getA( ll l, ll r ) {
	ll len = r - l + 1;
	ll i = pow2[len];
	r -= ( 1 << i );
	r ++;
	return max( A[i][l], A[i][r] );
}

ll getB( ll l, ll r ) {
	ll len = r - l + 1;
	ll i = pow2[len];
	r -= ( 1 << i );
	r ++;
	return min( B[i][l], B[i][r] );
}

ll getLA( ll i ) {
	if( la[i] != -1 )
		return la[i];
	ll l = 0;
	ll r = i;
	while( l != r ) {
		ll mid = ( l + r ) >> 1;
		if( getA( mid, i ) != a[i] )
			l = mid + 1;
		else
			r = mid;
	}
	return la[i] = r;
}

ll getRA( ll i ) {
	if( ra[i] != -1 )
		return ra[i];
	ll l = i;
	ll r = n-1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( getA( i, mid ) != a[i] )
			r = mid - 1;
		else
			l = mid;
	}
	return ra[i] = l;
}

ll getLB( ll i ) {
	if( lb[i] != -1 )
		return lb[i];
	ll l = 0;
	ll r = i;
	while( l != r ) {
		ll mid = ( l + r ) >> 1;
		if( getB( mid, i ) != b[i] )
			l = mid + 1;
		else
			r = mid;
	}
	return lb[i] = r;
}

ll getRB( ll i ) {
	if( rb[i] != -1 )
		return rb[i];
	ll l = i;
	ll r = n-1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( getB( i, mid ) != b[i] )
			r = mid - 1;
		else
			l = mid;
	}
	return rb[i] = l;
}

pair< ll, ll > findAinB( ll i ) {
	if( a[i] == b[i] )
		return mp( i, i );
	if( mb.find( a[i] ) == mb.end() )
		return mp( -1, -1 );
	ll idx = mb[a[i]];
	if( i <= v[idx][0] )
		return mp( -1, v[idx][0] );
	ll l = 0;
	ll r = v[idx].size() - 1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( v[idx][mid] <= i )
			l = mid;
		else
			r = mid - 1;
	}
	if( l == v[idx].size() - 1 )
		return mp( v[idx][l], -1 );
	return mp( v[idx][l], v[idx][l + 1] );
}

ll get( ll l, ll r, ll i, ll j ) {
	if( i > j )
		swap( i, j );
	ll a = i - l + 1;
	ll b = r - j + 1;
	ll res = a * b;
	return res;
}

bool add( ll i, ll j ) {
	if( i == -1 )
		return false;
	ll l = max( getLA( i ), getLB( j ) );
	l = max( l, ma[a[i]] );
	ll r = min( getRA( i ), getRB( j ) );
	if( l > r )
		return false;
	if( !in( l, r, i ) )
		return false;
	if( !in( l, r, j ) )
		return false;
	ans += get( l, r, i, j );
	return true;
}

ll solve() {
	del();
	forn( i, n ) {
		ll idx;
		if( mb.find( b[i] ) == mb.end() ) {
			idx = mb.size();
			mb[b[i]] = idx;
		} else {
			idx = mb[b[i]];
		}
		v[idx].pb( i );
		la[i] = ra[i] = -1;
		lb[i] = rb[i] = -1;
	}
	getTable();
	forn( i, n ) {
		pair< ll, ll > p = findAinB( i );
		ll L = p.X;
		ll R = p.Y;
		if( L == -1 && R == -1 )
			continue;
		if( add( i, L ) )
			ma[a[i]] = L + 1;
		if( L != R )
			add( i, R );
		ma[a[i]] = i + 1;
	}
	return ans;
}

ll solveBrute() {
	ll ans = 0;
	getTable();
	for( ll l = 0; l < n; l ++ ) {
		for( ll r = l; r < n; r ++ ) {
			ll mx = getA( l, r );
			ll mn = getB( l, r );
			if( mx == mn ) {
				ans ++;
			}
		}
	}
	return ans;
}

void checker() {
	while( true ) {
		genRandom( 5, 5 );
		ll res1 = solve();
		ll res2 = solveBrute();
		if( res1 != res2 ) {
			cout << "error: " << endl;
			cout << res1 << " " << res2 << endl;
			write();
			cin >> n;
		}
	}
}

int main( void ) {
	//checker();

	read();
	cout << solve() << endl;
	//cout << solveBrute() << endl;
	return 0;
}

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

5
3 2 3 2 3
3 4 3 4 3
*/


#include <bits/stdc++.h>

using namespace std;

#ifdef ONLINE_JUDGE
#define OJ 1
#else
#define OJ 0
#endif

#define dd				double
#define ll 				long long
#define pb 				push_back
#define mp 				make_pair
#define X				first
#define Y				second
#define forn( i, n ) 	for( ll i = 0; i < (ll) (n); i ++ )
#define endl 			'\n'

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

struct __s { __s() {
		srand( time( NULL ) );
		//freopen( "input.txt", "r", stdin ); freopen( "output.txt", "w", stdout );
		if( OJ ) { ios_base::Init i; cin.sync_with_stdio( 0 ); cin.tie( 0 ); }
	} ~__s() {
		if( !OJ ) TIMESTAMP;
		ll n; cin >> n;
	}
} __S;

ll n;
ll a[211111];
ll b[211111];

ll pow2[211111];

ll A[20][211111];
ll B[20][211111];

ll la[211111];
ll ra[211111];
ll lb[211111];
ll rb[211111];

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

void read() {
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
	}
}

void write() {
	cout << n << endl;
	forn( i, n ) {
		cout << a[i] << " ";
	}
	cout << endl;
	forn( i, n ) {
		cout << b[i] << " ";
	}
	cout << endl;
}

void del() {
	forn( i, mb.size() ) {
		v[i].clear();
	}
	ma.clear();
	mb.clear();
	ans = 0;
}

void genRandom( ll N, ll mod ) {
	n = N;
	forn( i, n ) {
		a[i] = rand()%mod;
		b[i] = rand()%mod;
	}
}

bool in( ll l, ll r, ll i ) {
	return ( l <= i && i <= r );
}

void getTable() {
	forn( i, n ) {
		A[0][i] = a[i];
		B[0][i] = b[i];

	}
	pow2[0] = 0;
	for( ll i = 1; i <= n; i ++ ) {
		pow2[i] = pow2[i-1];
		if( ( i & 1 ) == 0 )
			pow2[i] ++;
	}
	for( ll i = 1; i < 20; i ++ ) {
		ll d = ( 1 << ( i-1 ) );
		forn( j, n ) {
			A[i][j] = A[i-1][j];
			B[i][j] = B[i-1][j];
			ll k = j + d;
			if( k < n ) {
				A[i][j] = max( A[i][j], A[i-1][k] );
				B[i][j] = min( B[i][j], B[i-1][k] );
			}
		}
	}
}

ll getA( ll l, ll r ) {
	ll len = r - l + 1;
	ll i = pow2[len];
	r -= ( 1 << i );
	r ++;
	return max( A[i][l], A[i][r] );
}

ll getB( ll l, ll r ) {
	ll len = r - l + 1;
	ll i = pow2[len];
	r -= ( 1 << i );
	r ++;
	return min( B[i][l], B[i][r] );
}

ll getLA( ll i ) {
	if( la[i] != -1 )
		return la[i];
	ll l = 0;
	ll r = i;
	while( l != r ) {
		ll mid = ( l + r ) >> 1;
		if( getA( mid, i ) != a[i] )
			l = mid + 1;
		else
			r = mid;
	}
	return la[i] = r;
}

ll getRA( ll i ) {
	if( ra[i] != -1 )
		return ra[i];
	ll l = i;
	ll r = n-1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( getA( i, mid ) != a[i] )
			r = mid - 1;
		else
			l = mid;
	}
	return ra[i] = l;
}

ll getLB( ll i ) {
	if( lb[i] != -1 )
		return lb[i];
	ll l = 0;
	ll r = i;
	while( l != r ) {
		ll mid = ( l + r ) >> 1;
		if( getB( mid, i ) != b[i] )
			l = mid + 1;
		else
			r = mid;
	}
	return lb[i] = r;
}

ll getRB( ll i ) {
	if( rb[i] != -1 )
		return rb[i];
	ll l = i;
	ll r = n-1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( getB( i, mid ) != b[i] )
			r = mid - 1;
		else
			l = mid;
	}
	return rb[i] = l;
}

pair< ll, ll > findAinB( ll i ) {
	if( a[i] == b[i] )
		return mp( i, i );
	if( mb.find( a[i] ) == mb.end() )
		return mp( -1, -1 );
	ll idx = mb[a[i]];
	if( i <= v[idx][0] )
		return mp( -1, v[idx][0] );
	ll l = 0;
	ll r = v[idx].size() - 1;
	while( l != r ) {
		ll mid = ( l + r + 1 ) >> 1;
		if( v[idx][mid] <= i )
			l = mid;
		else
			r = mid - 1;
	}
	if( l == v[idx].size() - 1 )
		return mp( v[idx][l], -1 );
	return mp( v[idx][l], v[idx][l + 1] );
}

ll get( ll l, ll r, ll i, ll j ) {
	if( i > j )
		swap( i, j );
	ll a = i - l + 1;
	ll b = r - j + 1;
	ll res = a * b;
	return res;
}

bool add( ll i, ll j ) {
	if( i == -1 )
		return false;
	ll l = max( getLA( i ), getLB( j ) );
	l = max( l, ma[a[i]] );
	ll r = min( getRA( i ), getRB( j ) );
	if( l > r )
		return false;
	if( !in( l, r, i ) )
		return false;
	if( !in( l, r, j ) )
		return false;
	ans += get( l, r, i, j );
	return true;
}

ll solve() {
	del();
	forn( i, n ) {
		ll idx;
		if( mb.find( b[i] ) == mb.end() ) {
			idx = mb.size();
			mb[b[i]] = idx;
		} else {
			idx = mb[b[i]];
		}
		v[idx].pb( i );
		la[i] = ra[i] = -1;
		lb[i] = rb[i] = -1;
	}
	getTable();
	forn( i, n ) {
		pair< ll, ll > p = findAinB( i );
		ll L = p.X;
		ll R = p.Y;
		if( L == -1 && R == -1 )
			continue;
		if( add( i, L ) )
			ma[a[i]] = L + 1;
		if( L != R )
			add( i, R );
		ma[a[i]] = i + 1;
		if( n == 92 && i == 5 )
		return 0;
	}
	return ans;
}

ll solveBrute() {
	ll ans = 0;
	getTable();
	for( ll l = 0; l < n; l ++ ) {
		for( ll r = l; r < n; r ++ ) {
			ll mx = getA( l, r );
			ll mn = getB( l, r );
			if( mx == mn ) {
				ans ++;
			}
		}
	}
	return ans;
}

void checker() {
	while( true ) {
		genRandom( 5, 5 );
		ll res1 = solve();
		ll res2 = solveBrute();
		if( res1 != res2 ) {
			cout << "error: " << endl;
			cout << res1 << " " << res2 << endl;
			write();
			cin >> n;
		}
	}
}

int main( void ) {
	//checker();

	read();
	cout << solve() << endl;
	//cout << solveBrute() << endl;
	return 0;
}

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

5
3 2 3 2 3
3 4 3 4 3
*/


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l>r)
    {
        long long mid = l + (r-l)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = l + (r-l+1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;
int n, lg = 20;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spt1(N, vector<long long>(20, 0)), spt2(N, vector<long long>(20, 0));

void build()
{
    for(int i = 0; i<n; i++)
    {
        spt1[i][0] = a[i];
        spt2[i][0] = b[i];
    }

    p[0] = p[1] = 0;
    for(int i=2;i<N;i++)
        p[i] = 1 + p[i/2];

    for(int i=1;i<lg;i++)
    {
        for(int j=0; j<=n - (1<<i) + 1; j++)
        {
            spt1[j][i] = max(spt1[j][i-1], spt1[ j + (1<<(i-1)) ][i-1]);
            spt2[j][i] = min(spt2[j][i-1], spt2[j + (1<<(i-1))][i-1]);
        }
    }
}

long long getmax(int L, int R)
{/*
    long long k = p[r-l];
    return max(spt1[k][l], spt1[k][r - (1<<k)]);
    */
    long long ans = INT_MIN;
    for(int i = lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = max(ans, spt1[L][i]);
            L+=(1<<i);
        }
    }

    return ans;
}

long long getmin(int L, int R)
{/*
    long long k = p[r-l];
    return min(spt2[k][l], spt2[k][r-(1<<k)]);
    */
    long long ans  = INT_MAX;
    for(int i=lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = min(ans, spt2[L][i]);
            L+=(1<<i);
        }
    }
    return ans;
}

bool check(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax >= bmin;
}

bool check1(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax <= bmin;
}

long long compute(int pos)
{
    int l = pos, r = n;
    while(l < r)
    {
        int mid = l + (r-l)/2;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    long long left = l;
    l=pos, r = n;
    while(l < r)
    {
        int mid = (l+r+1)/2;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    long long right = l;

    return max(0LL, right - left + 1);
}

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

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

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

    build();
    long long ans = 0;

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

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n-1, left, right;
    while(l>r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n-1;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i); i++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l>r)
    {
        long long mid = l + (r-l)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = l + (r-l+1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;
int n, lg = 20;
vector<long long>a(N), b(N);
vector< vector<long long> >spt1(N, vector<long long>(20, 0)), spt2(N, vector<long long>(20, 0));

void build()
{
    for(int i = 1; i<=n; i++)
    {
        spt1[i][0] = a[i];
        spt2[i][0] = b[i];
    }

    for(int i=1;i<lg;i++)
    {
        for(int j=1; j<=n - (1<<i); j++)
        {
            spt1[j][i] = max(spt1[j][i-1], spt1[ j + (1<<(i-1)) ][i-1]);
            spt2[j][i] = min(spt2[j][i-1], spt2[j + (1<<(i-1))][i-1]);
        }
    }
}

long long getmax(int L, int R)
{
    long long ans = INT_MIN;
    for(int i = lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = max(ans, spt1[L][i]);
            L+=(1<<i);
        }
    }

    return ans;
}

long long getmin(int L, int R)
{
    long long ans  = INT_MAX;
    for(int i=lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = min(ans, spt2[L][i]);
            L+=(1<<i);
        }
    }
    return ans;
}

bool check(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax >= bmin;
}

bool check1(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax <= bmin;
}

long long compute(int pos)
{
    int l = pos, r = n;
    while(l < r)
    {
        int mid = l + (r-l)/2;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    long long left = l;
    l=pos, r = n;
    while(l < r)
    {
        int mid = (l+r+1)/2;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    long long right = l;

    return max(0LL, right - left + 1);
}

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

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

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

    build();
    long long ans = 0;

    for(int i=1; i<=n; i++)
    {
        ans += compute(i);
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n-1, left, right;
    while(l<r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n-1;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n-1, left, right;
    while(l>r)
    {
        long long mid = l + (r-l)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n-1;
    while(l<r)
    {
        long long mid = l + (r-l+1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;
int n, lg = 20;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spt1(N, vector<long long>(20, 0)), spt2(N, vector<long long>(20, 0));

void build()
{
    for(int i = 0; i<n; i++)
    {
        spt1[i][0] = a[i];
        spt2[i][0] = b[i];
    }

    p[0] = p[1] = 0;
    for(int i=2;i<N;i++)
        p[i] = 1 + p[i/2];

    for(int i=1;i<lg;i++)
    {
        for(int j=0; j<=n - (1<<i) + 1; j++)
        {
            spt1[j][i] = max(spt1[j][i-1], spt1[ j + (1<<(i-1)) ][i-1]);
            spt2[j][i] = min(spt2[j][i-1], spt2[j + (1<<(i-1))][i-1]);
        }
    }
}

long long getmax(int L, int R)
{/*
    long long k = p[r-l];
    return max(spt1[k][l], spt1[k][r - (1<<k)]);
    */
    long long ans = INT_MIN;
    for(int i = lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = max(ans, spt1[L][i]);
            L+=(1<<i);
        }
    }

    return ans;
}

long long getmin(int L, int R)
{/*
    long long k = p[r-l];
    return min(spt2[k][l], spt2[k][r-(1<<k)]);
    */
    long long ans  = INT_MAX;
    for(int i=lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = min(ans, spt2[L][i]);
            L+=(1<<i);
        }
    }
    return ans;
}

bool check(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax >= bmin;
}

bool check1(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax <= bmin;
}

long long compute(int pos)
{
    int l = pos, r = n;
    while(r-l > 1)
    {
        int mid = l + (r-l)/2;
        if(check(pos, mid))
            r = mid;
        else
            l = mid;
    }
    if(!check(pos, l))
        return 0;
    long long left = l;
    l=pos, r = n;
    while(r-l >1)
    {
        int mid = (l+r)/2;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid;
    }
    if(!check1(pos, l))
        return 0;
    long long right = l;

    return max(0LL, right - left + 1);
}

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

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

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

    build();
    long long ans = 0;

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

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i)+1; j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l<r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=1; i<=n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=1; j<=n - (1<<i)+1; j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l<r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

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

    build();
    long long ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;
int n, lg = 20;
vector<long long>a(N), b(N);
vector< vector<long long> >spt1(N, vector<long long>(20, 0)), spt2(N, vector<long long>(20, 0));

void build()
{
    for(int i = 1; i<=n; i++)
    {
        spt1[i][0] = a[i];
        spt2[i][0] = b[i];
    }

    for(int i=1;i<lg;i++)
    {
        for(int j=1; j<=n - (1<<i); j++)
        {
            spt1[j][i] = max(spt1[j][i-1], spt1[ j + (1<<(i-1)) ][i-1]);
            spt2[j][i] = min(spt2[j][i-1], spt2[j + (1<<(i-1))][i-1]);
        }
    }
}

long long getmax(int L, int R)
{
    long long ans = INT_MIN;
    for(int i = lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = max(ans, spt1[L][i]);
            L+=(1<<i);
        }
    }

    return ans;
}

long long getmin(int L, int R)
{
    long long ans  = INT_MAX;
    for(int i=lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = min(ans, spt2[L][i]);
            L+=(1>>i);
        }
    }
    return ans;
}

bool check(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax >= bmin;
}

bool check1(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax <= bmin;
}

long long compute(int pos)
{
    int l = pos, r = n;
    while(l < r)
    {
        int mid = l + (r-l)/2;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    long long left = l;
    l=pos, r = n;
    while(l < r)
    {
        int mid = l + (r-l)/2;
        if(!check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    long long right = l;

    return max(0LL, right - left + 1);
 }

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

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

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

    build();
    long long ans = 0;

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

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=1; i<=n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=1; j<=n - (1<<i)+1; j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

// long long getMin(long long l, long long r)
// {
//     int d = p[r-l];
//     return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
// }

// long long getMax(int l, int r)
// {
//     int d = p[r-l];
//     return max(spta[d][l], spta[d][ r - (1<<d)+1]);
// }
long long getMax(long long L, long long R)
{
    long long answer=INT_MIN;
    for(long long j=lg-1;j>=0;j--)
    {
        if(L + (1<<j) - 1 <= R)
        {
            answer=max(answer, spta[j][L]);
            L+=(1<<j);
        }
    }
    return answer;
}

long long getMin(long long L, long long R)
{
    long long answer=INT_MAX;
    for(long long j=lg-1;j>=0;j--)
    {
        if(L + (1<<j) - 1 <= R)
        {
            answer=min(answer, sptb[j][L]);
            L+=(1<<j);
        }
    }
    return answer;
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l<r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

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

    build();
    long long ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=1; i<=n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=1; j<=n - (1<<i)+1; j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

// long long getMin(long long l, long long r)
// {
//     int d = p[r-l];
//     return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
// }

// long long getMax(int l, int r)
// {
//     int d = p[r-l];
//     return max(spta[d][l], spta[d][ r - (1<<d)+1]);
// }
long long getMax(long long L, long long R)
{
    long long answer=INT_MIN;
    for(long long j=lg-1;j>=0;j--)
    {
        if(L + (1<<j) - 1 <= R)
        {
            answer=max(answer, spta[j][L]);
            L+=(1<<j);
        }
    }
    return answer;
}

long long getMin(long long L, long long R)
{
    long long answer=INT_MAX;
    for(long long j=lg-1;j>=0;j--)
    {
        if(L + (1<<j) - 1 <= R)
        {
            answer=min(answer, sptb[j][L]);
            L+=(1<<j);
        }
    }
    return answer;
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l>r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

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

    build();
    long long ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=1; i<=n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=1; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l>r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

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

    build();
    long long ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=1; i<=n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=1; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l<r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

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

    build();
    long long ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=0; i<n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=0; j<=n - (1<<i); j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l<r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        cin>>b[i];

    build();
    long long ans = 0;
    for(int i=0; i<n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;
int n, lg = 20;
vector<long long>a(N), b(N);
vector< vector<long long> >spt1(N, vector<long long>(20, 0)), spt2(N, vector<long long>(20, 0));

void build()
{
    for(int i = 1; i<=n; i++)
    {
        spt1[i][0] = a[i];
        spt2[i][0] = b[i];
    }

    for(int i=1;i<lg;i++)
    {
        for(int j=1; j<=n - (1<<i) + 1; j++)
        {
            spt1[j][i] = max(spt1[j][i-1], spt1[ j + (1<<(i-1)) ][i-1]);
            spt2[j][i] = min(spt2[j][i-1], spt2[j + (1<<(i-1))][i-1]);
        }
    }
}

long long getmax(int L, int R)
{
    long long ans = INT_MIN;
    for(int i = lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = max(ans, spt1[L][i]);
            L+=(1<<i);
        }
    }

    return ans;
}

long long getmin(int L, int R)
{
    long long ans  = INT_MAX;
    for(int i=lg-1; i>=0; i--)
    {
        if(L + (1<<i) - 1 <= R)
        {
            ans = min(ans, spt2[L][i]);
            L+=(1<<i);
        }
    }
    return ans;
}

bool check(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax >= bmin;
}

bool check1(int l, int r)
{
    long long amax = getmax(l, r);
    long long bmin = getmin(l, r);

    return amax <= bmin;
}

long long compute(int pos)
{
    int l = pos, r = n;
    while(l < r)
    {
        int mid = l + (r-l)/2;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    long long left = l;
    l=pos, r = n;
    while(l < r)
    {
        int mid = (l+r+1)/2;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    long long right = l;

    return max(0LL, right - left + 1);
}

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

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

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

    build();
    long long ans = 0;

    for(int i=1; i<=n; i++)
    {
        ans += compute(i);
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 2e5+5;

long long n, lg = 19;
vector<long long>a(N), b(N), p(N);
vector< vector<long long> >spta(lg, vector<long long>(N, 0)), sptb(lg, vector<long long>(N, 0));

void build()
{
    for(int i=1; i<=n; i++)
    {
        spta[0][i] = a[i];
        sptb[0][i] = b[i];
    }

    for(int i=2; i<N; i++)
        p[i] = p[i/2] + 1;

    for(int i=1; i<lg; i++)
    {
        for(int j=1; j<=n - (1<<i) + 1; j++)
        {
            spta[i][j] = max(spta[i-1][j], spta[i-1][j + (1<<(i-1))]);
            sptb[i][j] = min(sptb[i-1][j], sptb[i-1][j + (1<<(i-1))]);
        }
    }
}

long long getMin(long long l, long long r)
{
    int d = p[r-l];
    return min(sptb[d][l], sptb[d][ r - (1<<d)+1]);
}

long long getMax(int l, int r)
{
    int d = p[r-l];
    return max(spta[d][l], spta[d][ r - (1<<d)+1]);
}

bool check(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma>=mi;
}

bool check1(long long l, long long r)
{
    long long ma = getMax(l, r);
    long long mi = getMin(l, r);

    return ma<=mi;
}

long long compute(long long pos)
{
    long long l = pos, r = n, left, right;
    while(l>r)
    {
        long long mid = (l + r)/2LL;
        if(check(pos, mid))
            r = mid;
        else
            l = mid+1;
    }
    if(!check(pos, l))
        return 0;
    left = l;
    l = pos, r = n;
    while(l<r)
    {
        long long mid = (l + r + 1)/2LL;
        if(check1(pos, mid))
            l = mid;
        else
            r = mid-1;
    }
    if(!check1(pos, l))
        return 0;
    right = l;

    return max(0LL, right - left + 1);
}

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

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

    build();
    long long ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans+=compute(i);
    }
    cout<<ans;
    // int l, r;
    // cin>>l>>r;
    // cout<<getMax(l, r)<<'\n'<<getMin(l,r);

    return 0;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+7;
const int lgN=23;
int a[maxn],b[maxn],rmnq[maxn][lgN],rmxq[maxn][lgN],lg[maxn],n,res;

void precomput(){
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	lg[1]=0;
	for(int i=2;i<maxn;i++)lg[i]=lg[i/2]+1;
    for(int i=0;i<n;i++)rmxq[i][0]=a[i];
    for(int i=0;i<n;i++)rmnq[i][0]=b[i];

	for(int j=1;j<lgN;j++)
		for(int i=0;i+(1<<j)<=n;i++)
			rmnq[i][j]=min(rmnq[i][j-1],rmnq[i+(1<<(j-1))][j-1]);

	for(int j=1;j<lgN;j++)
		for(int i=0;i+(1<<j)<=n;i++)
			rmxq[i][j]=max(rmxq[i][j-1],rmxq[i+(1<<(j-1))][j-1]);
}

int mn(int l,int r){
    int x=lg[r-l];
    return min(rmnq[l][x],rmnq[r-(1<<x)][x]);
}

int mx(int l,int r){
    int x=lg[r-l];
    return max(rmxq[l][x],rmxq[r-(1<<x)][x]);
}

int lower(int l,int r=n+1){
	int bgn=l;
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(bgn,mid)<=mn(bgn,mid))l=mid;
		else r=mid;
	}
	return l;
}

int upper(int l,int r=n+1){
	int bgn=l;
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(bgn,mid)<mn(bgn,mid))l=mid;
		else r=mid;
	}
	return l;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	precomput();
	for(int i=0;i<n;i++)res+=lower(i)-upper(i);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+7;
const int lgN=23;
int a[maxn],b[maxn],rmnq[maxn][lgN],rmxq[maxn][lgN],lg[maxn],n,res;

void precomput(){
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	lg[1]=0;
	for(int i=2;i<maxn;i++)lg[i]=lg[i/2]+1;
    for(int i=0;i<n;i++)rmxq[i][0]=a[i];
    for(int i=0;i<n;i++)rmnq[i][0]=b[i];

	for(int j=1;j<lgN;j++)
		for(int i=0;i+(1<<j)<=n;i++)
			rmnq[i][j]=min(rmnq[i][j-1],rmnq[i+(1<<(j-1))][j-1]);

	for(int j=1;j<lgN;j++)
		for(int i=0;i+(1<<j)<=n;i++)
			rmxq[i][j]=max(rmxq[i][j-1],rmxq[i+(1<<(j-1))][j-1]);
}

int mn(int l,int r){
	int x=lg[r-l];
	return min(rmnq[l][x],rmnq[r-(1<<x)][x]);
}
int mx(int l,int r){
	int x=lg[r-l];
	return max(rmxq[l][x],rmxq[r-(1<<x)][x]);
}

int lower(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<=mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int upper(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	precomput();
	for(int i=0;i<n;i++)res+=lower(i)-upper(i);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+7;
const int lgN=23;
int a[maxn],b[maxn],rmnq[maxn][lgN],rmxq[maxn][lgN],lg[maxn],n,res;

void precomput(){
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	lg[1]=0;
	for(int i=2;i<maxn;i++)lg[i]=lg[i/2]+1;
    for(int i=0;i<n;i++)rmxq[i][0]=a[i];
    for(int i=0;i<n;i++)rmnq[i][0]=b[i];

	for(int j=1;j<lgN;j++)
		for(int i=0;i+(1<<j)<=n;i++)
			rmnq[i][j]=min(rmnq[i][j-1],rmnq[i+(1<<(j-1))][j-1]);

	for(int j=1;j<lgN;j++)
		for(int i=0;i+(1<<j)<=n;i++)
			rmxq[i][j]=max(rmxq[i][j-1],rmxq[i+(1<<(j-1))][j-1]);
}

int mn(int l,int r){
    int x=lg[r-l];
    return min(rmnq[l][x],rmnq[r-(1<<x)][x]);
}

int mx(int l,int r){
    int x=lg[r-l];
    return max(rmxq[l][x],rmxq[r-(1<<x)][x]);
}

int lower(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<=mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int upper(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	precomput();
	for(int i=0;i<n;i++)res+=lower(i)-upper(i);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+7;
const int lgN=23;
int a[maxn],b[maxn],rmnq[maxn][lgN],rmxq[maxn][lgN],lg[maxn],n,res;

void precomput(){
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	for(int i=2;i<maxn;i++)lg[i]=lg[i/2]+1;
	for(int i=0;i<n;i++)rmnq[i][0]=b[i];
	for(int i=0;i<n;i++)rmxq[i][0]=a[i];

	for(int j=1;j<lgN;j++)
		for(int i=0;i<n&&i+(1<<j)<=n;i++)
			rmnq[i][j]=min(rmnq[i][j-1],rmnq[i+(1<<(j-1))][j-1]);

	for(int j=1;j<lgN;j++)
		for(int i=0;i<n&&i+(1<<j)<=n;i++)
			rmxq[i][j]=max(rmxq[i][j-1],rmxq[i+(1<<(j-1))][j-1]);

	return ;
}

int mn(int l,int r){
	int x=lg[r-l];
	return min(rmnq[l][x],rmnq[r-x][x]);
}
int mx(int l,int r){
	int x=lg[r-l];
	return max(rmxq[l][lg[r-l]],rmxq[r-(1<<lg[r-l])][lg[r-l]]);
}

int lower(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<=mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int upper(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	precomput();
	for(int i=0;i<n;i++)res+=lower(i)-upper(i);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+7;
const int lgN=23;
int a[maxn],b[maxn],rmnq[maxn][lgN],rmxq[maxn][lgN],lg[maxn],n,res;

void precomput(){
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	lg[1]=0;
	for(int i=2;i<maxn;i++)lg[i]=lg[i/2]+1;
	for(int i=0;i<n;i++)rmnq[i][0]=b[i];
	for(int i=0;i<n;i++)rmxq[i][0]=a[i];

	for(int j=1;j<lgN;j++)
		for(int i=0;i<n&&i+(1<<j)<=n;i++)
			rmnq[i][j]=min(rmnq[i][j-1],rmnq[i+(1<<j)/2][j-1]);

	for(int j=1;j<lgN;j++)
		for(int i=0;i<n&&i+(1<<j)<=n;i++)
			rmxq[i][j]=max(rmxq[i][j-1],rmxq[i+(1<<j)/2][j-1]);

	return ;
}

int mn(int l,int r){
	int x=lg[r-l];
	return min(rmnq[l][x],rmnq[r-(1<<x)][x]);
}
int mx(int l,int r){
	int x=lg[r-l];
	return max(rmxq[l][x],rmxq[r-(1<<x)][x]);
}

int lower(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<=mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int upper(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	precomput();
	for(int i=0;i<n;i++)res+=lower(i)-upper(i);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define ld long double
#define int long long
#define F first
#define S second
using namespace std;
void null(){return ;}
const int INF=3e18+9237;
const int maxn=2e5+7;
const int mod=1e9+7;
const int sqN=320;
const int lgN=23;
int a[maxn],b[maxn],rmnq[maxn][lgN],rmxq[maxn][lgN],lg[maxn],n,res;

void precomput(){
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	for(int i=2;i<maxn;i++)lg[i]=lg[i/2]+1;
	for(int i=0;i<n;i++)rmnq[i][0]=b[i];
	for(int i=0;i<n;i++)rmxq[i][0]=a[i];

	for(int j=1;j<lgN;j++)
		for(int i=0;i<n&&(1<<j)<n+1;i++)
			rmnq[i][j]=min(rmnq[i][j-1],rmnq[i+(1<<j)/2][j-1]);

	for(int j=1;j<lgN;j++)
		for(int i=0;i<n&&(1<<j)<n+1;i++)
			rmxq[i][j]=max(rmxq[i][j-1],rmxq[i+(1<<j)/2][j-1]);

	return ;
}

int mn(int l,int r){
	return min(rmnq[l][lg[r-l]],rmnq[r-(1<<lg[r-l])][lg[r-l]]);
}
int mx(int l,int r){
	return max(rmxq[l][lg[r-l]],rmxq[r-(1<<lg[r-l])][lg[r-l]]);
}

int lower(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<=mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int upper(int l,int r=n+1){
	while(r-l>1){
		int mid=l+r>>1;
		if(mx(l,mid)<mn(l,mid))l=mid;
		else r=mid;
	}
	return l;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	precomput();
	for(int i=0;i<n;i++)res+=lower(i)-upper(i);
	cout<<res;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=5e5+10,inf=1e9+10;
int a[M],b[M];
int rmax[M][30],rmin[M][30];
main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=1;i<=4e5;i++)
	{
		for(int j=0;j<=19;j++)
			rmax[i][j]=-inf,rmin[i][j]=inf;
	}
	for(int i=n;i>=1;i--)
	{
		rmax[i][0]=a[i];
		rmin[i][0]=b[i];
		for(int j=1;j<=19;j++)
		{
			rmax[i][j]=max(rmax[i][j-1],rmax[i+(1<<(j-1))][j-1]);
			rmin[i][j]=min(rmin[i][j-1],rmin[i+(1<<j-1)][j-1]);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int maxx=a[i],minn=b[i],ind=i;
	//	if(maxx>minn)
	//		continue;
		for(int j=19;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		int ind2=ind;
	    maxx=a[i],minn=b[i],ind=i;
		for(int j=19;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>=0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		if(ind2==i && a[i]==b[i])
			ind2--;
		ans+=(ind-ind2);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=5e5+10;
int a[M],b[M];
int rmax[M][30],rmin[M][30];
main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=n;i>=1;i--)
	{
		rmax[i][0]=a[i];
		rmin[i][0]=b[i];
		for(int j=1;j<=19;j++)
		{
			rmax[i][j]=max(rmax[i][j-1],rmax[i+(1<<(j-1))][j-1]);
			rmin[i][j]=min(rmin[i][j-1],rmin[i+(1<<j-1)][j-1]);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int maxx=a[i],minn=b[i],ind=i;
		if(maxx>minn)
			continue;
		for(int j=19;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		int ind2=ind;
	    maxx=a[i],minn=b[i],ind=i;
		for(int j=19;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>=0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		if(ind2==i && a[i]==b[i])
			ind2--;
		ans+=(ind-ind2);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=5e5+10,inf=1e9+10;
int a[M],b[M];
int rmax[M][30],rmin[M][30];
main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=1;i<=4e5;i++)
	{
		for(int j=0;j<=19;j++)
			rmax[i][j]=-inf,rmin[i][j]=inf;
	}
	for(int i=n;i>=1;i--)
	{
		rmax[i][0]=a[i];
		rmin[i][0]=b[i];
		for(int j=1;j<=19;j++)
		{
			rmax[i][j]=max(rmax[i][j-1],rmax[i+(1<<(j-1))][j-1]);
			rmin[i][j]=min(rmin[i][j-1],rmin[i+(1<<j-1)][j-1]);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int maxx=-inf,minn=inf,ind=i;
	//	if(maxx>minn)
	//		continue;
		for(int j=19;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>0 && (ind+(1<<j))<=n+1)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j);
			}
		}
		int ind2=ind;
	    maxx=-inf,minn=inf,ind=i;
		for(int j=19;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>=0 && (ind+(1<<j))<=n+1)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j);
			}
		}
		ans+=(ind-ind2);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=5e5+10;
int a[M],b[M];
int rmax[M][30],rmin[M][30];
main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=n;i>=1;i--)
	{
		rmax[i][0]=a[i];
		rmin[i][0]=b[i];
		for(int j=1;j<18;j++)
		{
			rmax[i][j]=max(rmax[i][j-1],rmax[i+(1<<(j-1))][j-1]);
			rmin[i][j]=min(rmin[i][j-1],rmin[i+(1<<j-1)][j-1]);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int maxx=a[i],minn=b[i],ind=i;
		if(maxx>minn)
			continue;
		for(int j=17;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		int ind2=ind;
	    maxx=a[i],minn=b[i],ind=i;
		for(int j=17;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>=0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		if(ind2==i && a[i]==b[i])
			ind2--;
		ans+=(ind-ind2);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
int const M=2e5+10;
int a[M],b[M];
int rmax[M][30],rmin[M][30];
main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=n;i>=1;i--)
	{
		rmax[i][0]=a[i];
		rmin[i][0]=b[i];
		for(int j=1;j<18;j++)
		{
			rmax[i][j]=max(rmax[i][j-1],rmax[i+(1<<(j-1))][j-1]);
			rmin[i][j]=min(rmin[i][j-1],rmin[i+(1<<j-1)][j-1]);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int maxx=a[i],minn=b[i],ind=i;
		if(maxx>minn)
			continue;
		for(int j=17;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		int ind2=ind;
	    maxx=a[i],minn=b[i],ind=i;
		for(int j=17;j>=0;j--)
		{
			if((min(minn,rmin[ind][j])-max(maxx,rmax[ind][j]))>=0 && (ind+(1<<j)-1)<=n)
			{
				minn=min(rmin[ind][j],minn),maxx=max(maxx,rmax[ind][j]);
				ind+=(1<<j)-1;
			}
		}
		if(ind2==i && a[i]==b[i])
			ind2--;
		ans+=(ind-ind2);
	}
	cout<<ans;
}


/* 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 = l;

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


#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;
ll 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 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 = l;

		for(int k = LG-1 ; k >= 0 ; k--)
		{
			if( ( min( MI , mn[k][r] ) == max( MA , mx[k][r] ) ) && ( r + (1<<k) <= n+1 ) )
				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 = 5e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll 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+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));
			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 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 = l;
		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;
}


/* 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( r == i-1 )
			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+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 << l << " " << r <<"\n";
		ANS += r-l;
	}
	if( ANS < 0 )
		return cout << 0 , 0;
	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( r + (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( l + (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 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 = l;
		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][r] ) , MA = max( MA , mx[k][r] ) , r += (1<<k);
		}
		ANS += r-l;
	}
	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));
			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;

	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;
		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;
	}

	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] ) );
	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 )+1, 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( 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 ll INF = 4e18;
const int MAX = 5e5;
const int MOD = 1e9 + 7;
const int LG = 20;
ll 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+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));
			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+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;
}