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

int n,m;
int a[123],b[123];
int c[123];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        while(a[i]<=b[i]){
           c[a[i]]++;
           a[i]++;
        }
    }

    // if(checK()){
        // cout<<"OK"<<endl;
    // }
    for(int i=1;i<=n;i++)
    {
        if(c[i]!=1)
        {
            cout<<i<<" "<<c[i]<<endl;
            exit(0);
        }
    }
    cout<<"OK"<<endl;
    return 0;
}


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

int n,m;
int a,b;
int c[123];
int main()
{
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b;
        while(a<=b){
           c[a]++;
           a++;
        }
    }

    // if(checK()){
        // cout<<"OK"<<endl;
    // }
    for(int i=1;i<=n;i++)
    {
        if(c[i]!=1)
        {
            cout<<i<<" "<<c[i]<<endl;
            exit(0);
        }
    }
    cout<<"OK"<<endl;
    return 0;
}


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

int n,m;
int a[123],b[123];
int c[12300];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        while(a[i]<b[i]){
           c[a[i]]++;
           a[i]++;
        }
    }

    // if(checK()){
        // cout<<"OK"<<endl;
    // }
    for(int i=1;i<=n;i++)
    {
        if(c[i]!=1)
        {
            cout<<i<<" "<<c[i]<<endl;
            exit(0);
        }
    }
    cout<<"OK"<<endl;
    return 0;
}


#include<bits/stdc++.h>

typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define pie 3.1415926536
ll mod = 1000000007;

using namespace std;

int main()
{
	fast;
	int n,m;
	cin >> n >> m;
	    int x[1001]={0};
	while(m--)
	{
	    int a,b;
	    cin >> a >> b;
	    for(int i=a;i<=b;i++)
	    x[i]++;
	}
	    for(int i=1;i<=n;i++)
	    {
	        if(x[i]!=1)
	        {
	            cout << i << " " << x[i];
	            return 0;
	        }
	    }

	cout << "OK";
    return 0;
}


/*
	codeforces problemset: 44, C
	Holidays
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	int pre_v = 0;
	int state = 0;
	int count = 0;
	for(int i=0; i<m; i++)
	{
		int t1, t2;
		cin >> t1 >> t2;
		//cout << t1 << ", " << t2 << endl;

		switch(state)
		{
			case 0:
				if(t1 == pre_v)
				{
					if(t1 != t2)
					{
						cout << t1 << " 2" << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						state = 1;
						count = 2;
					}
				}
				else if(t1 == pre_v + 1)
				{
				    pre_v=t2;
				}
				else if(t1 > pre_v + 1)
				{
					cout << pre_v + 1 << " 0" << endl;
					exit(0);
				}

			break;
			case 1:
				if(t1==pre_v)
				{
					if(t1 != t2)
					{
						cout << pre_v << " " << count << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						count++;
					}
				}
				else if(t1 != pre_v)
				{
					cout << pre_v << " " << count << endl;
					exit(0);
				}
		}
	}

	if(state == 1)
	{
		cout << pre_v << " " << count << endl;
		exit(0);
	}

	if(pre_v < n)
	{
		cout << pre_v + 1 << " 0" << endl;
		exit(0);
	}

	cout << "OK" << endl;

	return 0;
}


/*
	codeforces problemset: 44, C
	Holidays
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	int pre_v = 0;
	int state = 0;
	int count = 0;
	for(int i=0; i<m; i++)
	{
		int t1, t2;
		cin >> t1 >> t2;
		//cout << t1 << ", " << t2 << endl;

		switch(state)
		{
			case 0:
				if(t1 == pre_v)
				{
					if(t1 != t2)
					{
						cout << t1 << " 2" << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						state = 1;
						count = 2;
					}
				}
				else if(t1 == pre_v + 1)
				{
				    pre_v=t2;
				}
				else if(t1 > pre_v + 1)
				{
					cout << pre_v + 1 << " 0" << endl;
					exit(0);
				}

			break;
			case 1:
				if(t1==pre_v)
				{
				    count++;
					if(t1 != t2)
					{
						cout << pre_v << " " << count << endl;
						exit(0);
					}
				}
				else if(t1 != pre_v)
				{
					cout << pre_v << " " << count << endl;
					exit(0);
				}
		}
	}

	if(state == 1)
	{
		cout << pre_v << " " << count << endl;
		exit(0);
	}

	cout << "OK" << endl;

	return 0;
}


/*
	codeforces problemset: 44, C
	Holidays
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	int pre_v = 0;
	int state = 0;
	int count = 0;
	for(int i=0; i<m; i++)
	{
		int t1, t2;
		cin >> t1 >> t2;
		//cout << t1 << ", " << t2 << endl;

		switch(state)
		{
			case 0:
				if(t1 == pre_v)
				{
					if(t1 != t2)
					{
						cout << t1 << " 2" << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						state = 1;
						count = 2;
					}
				}
				else if(t1 == pre_v + 1)
				{
				    pre_v=t2;
				}
				else if(t1 > pre_v + 1)
				{
					cout << pre_v + 1 << " 0" << endl;
					exit(0);
				}

			break;
			case 1:
				if(t1==pre_v)
				{
					if(t1 != t2)
					{
						cout << pre_v << " " << count << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						count++;
					}
				}
				else if(t1 != pre_v)
				{
					cout << pre_v << " " << count << endl;
					exit(0);
				}
		}
	}

	cout << "OK" << endl;

	return 0;
}


/*
	codeforces problemset: 44, C
	Holidays
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	int pre_v = 0;
	int state = 0;
	int count = 0;
	for(int i=0; i<m; i++)
	{
		int t1, t2;
		cin >> t1 >> t2;
		//cout << t1 << ", " << t2 << endl;

		switch(state)
		{
			case 0:
				if(t1 == pre_v)
				{
					if(t1 != t2)
					{
						cout << t1 << " 2" << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						state = 1;
						count = 2;
					}
				}
				else if(t1 == pre_v + 1)
				{
				    pre_v=t2;
				}
				else if(t1 > pre_v + 1)
				{
					cout << pre_v + 1 << " 0" << endl;
					exit(0);
				}

			break;
			case 1:
				if(t1==pre_v)
				{
					if(t1 != t2)
					{
						cout << pre_v << " " << count << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						count++;
					}
				}
				else if(t1 != pre_v)
				{
					cout << pre_v << " " << count << endl;
					exit(0);
				}
		}
	}

	if(state == 1)
	{
		cout << pre_v << " " << count << endl;
		exit(0);
	}

	cout << "OK" << endl;

	return 0;
}


/*
	codeforces problemset: 44, C
	Holidays
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	int pre_v = 0;
	int state = 0;
	int count = 0;
	for(int i=0; i<m; i++)
	{
		int t1, t2;
		cin >> t1 >> t2;
		//cout << t1 << ", " << t2 << endl;

		switch(state)
		{
			case 0:
				if(t1 == pre_v)
				{
					if(t1 != t2)
					{
						cout << t1 << " 2" << endl;
						exit(0);
					}
					else if(t1 == t2)
					{
						state = 1;
						count = 2;
					}
				}
				else if(t1 == pre_v + 1)
				{
				    pre_v=t2;
				}
				else if(t1 > pre_v + 1)
				{
					cout << pre_v + 1 << " 0" << endl;
					exit(0);
				}

			break;
			case 1:
				if(t1==pre_v)
				{
					count++;
					if(t1 != t2)
					{
						cout << pre_v << " " << count << endl;
						exit(0);
					}
				}
				else if(t1 != pre_v)
				{
					cout << pre_v << " " << count << endl;
					exit(0);
				}
		}
	}

	if(state == 1)
	{
		cout << pre_v << " " << count << endl;
		exit(0);
	}

	if(pre_v < n)
	{
		cout << pre_v + 1 << " 0" << endl;
		exit(0);
	}

	cout << "OK" << endl;

	return 0;
}


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

int main()
{

int n,m,i,x,y;
scanf("%d%d",&n,&m);
vector<int> a(n);

for(i=0;i<n;i++)
	a.push_back(0);

while(m--)
{
	scanf("%d%d",&x,&y);
	for(i=x;i<=y;i++)
		a[i]++;

}
for(i=1;i<=n;i++)
{
	if(a[i]!=1)
	{
	cout<<i<<" "<<a[i];
	return 0;
	}
}
cout<<"OK";

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repp(i,a,b) for(int i=a;i<=b;i++)
signed main()
{
    fast
    int n,m,flag=0,ans=0;
    cin>>n>>m;
    vector<int> v;
    while(m--){
        int a,b;
        cin>>a>>b;
        if(flag==0){
            if(a==ans){
                v.pb(ans);
                flag=1;
            }
            else if(a-ans>1){
                v.pb(ans+1);
                flag=2;
            }
            else
            {
                ans=b;
            }
        }
    }
    if(flag==1){
        cout<<v[0]<<" "<<2;
    }
    else if(flag==2){
        cout<<v[0]<<" "<<0;
    }
    else
    {
        cout<<"OK";
    }

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repp(i,a,b) for(int i=a;i<=b;i++)
signed main()
{
    fast
    int n,m,flag=0,ans=0,count=0;
    cin>>n>>m;
    vector<int> v;
    vector<pair<int,int> > x;
    while(m--){
        int a,b;
        cin>>a>>b;
        x.pb({a,b});
        if(flag==0){
            if(a==ans){
                v.pb(ans);
                flag=1;
            }
            else if(a-ans>1){
                v.pb(ans+1);
                flag=2;
            }
            else
            {
                ans=b;
            }
        }
    }
    if(flag==1){
        rep(i,0,x.size()){
            if(x[i].ff==ans or x[i].ss==ans){
                count++;
            }
        }
    }
    if(flag==1){
        cout<<v[0]<<" "<<count;
    }
    else if(flag==2){
        cout<<v[0]<<" "<<0;
    }
    else if(x[x.size()-1].ss!=n)
    {
        cout<<x[x.size()-1].ss+1<<" "<<0;
    }
    else if(x[0].ff!=1){
        cout<<1<<" "<<0;
    }
    else
    cout<<"OK";

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repp(i,a,b) for(int i=a;i<=b;i++)
signed main()
{
    fast
    int n,m,flag=0,ans=0,count=0;
    cin>>n>>m;
    vector<int> v;
    vector<pair<int,int> > x;
    while(m--){
        int a,b;
        cin>>a>>b;
        x.pb({a,b});
        if(flag==0){
            if(a==ans){
                v.pb(ans);
                flag=1;
            }
            else if(a-ans>1){
                v.pb(ans+1);
                flag=2;
            }
            else
            {
                ans=b;
            }
        }
    }
    if(flag==1){
        rep(i,0,x.size()){
            if(x[i].ff==ans or x[i].ss==ans){
                count++;
            }
        }
    }
    if(flag==1){
        cout<<v[0]<<" "<<count;
    }
    else if(flag==2){
        cout<<v[0]<<" "<<0;
    }
    else
    {
        cout<<"OK";
    }

}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,m,i,j;
    cin>>n>>m;
    int a[m][2];
    for(i=0;i<m;i++){
        cin>>a[i][0]>>a[i][1];
    }
    int b[n]={0};
    for(i=0;i<m;i++){
        for(j=a[i][0];j<=a[i][1];j++){
            b[j-1]++;
        }
    }
    for(i=0;i<n;i++){
        if(b[i]!=1){
            cout<<i+1<<" "<<b[i];
            break;
        }
    }
    if(i==n){
        cout<<"OK";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool compare(const pair<int,int> &a,const pair<int,int> &b)
{if(a.first==b.first)
 return a.second<b.second;
 else
 return a.first<b.first;
}
int main()
{int n,m,i,flag=0,f=0;
 ios_base::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
 cin>>n>>m;
 vector< pair<int,int> > v(m);
 set<int> sp;
 map<int,int> mp;
 set<int>::iterator itrr;
 map<int,int>::iterator itr;
 for(i=0;i<m;++i)
 cin>>v[i].first>>v[i].second;
 sort(v.begin(),v.end(),compare);
 //cout<<"\n...\n";
 //for(i=0;i<m;++i)
 //cout<<v[i].first<<" "<<v[i].second<<"\n";
 if(v[0].first!=1)
 {f=1;sp.insert(1);}
 for(i=0;i<m-1;++i)
 {if(v[i].second>=v[i+1].first)
  {++mp[v[i+1].first];
   //cout<<"abcd\n";
   flag=1;
  }
  else if(v[i+1].first>v[i].second+1)
  {f=1;
   sp.insert(v[i].second+1);
  }
 }
 if(v[m-1].second!=n)
 {f=1;sp.insert(v[m-1].second+1);}
 //cout<<"\n...\n";
 //for(itr=mp.begin();itr!=mp.end();++itr)
 //cout<<itr->first<<" "<<itr->second<<"\n";
 //cout<<"\n...\n";
 //for(itrr=sp.begin();itrr!=sp.end();++itrr)
 //cout<<*itrr<<"\n";
 if(flag && f)
 {itr=mp.begin();
  itrr=sp.begin();
  if(itr->first<*itrr)
  cout<<itr->first<<" "<<itr->second+1;
  else
  cout<<*itrr<<" 0";
 }
 else if(flag)
 {itr=mp.begin();
  cout<<itr->first<<" "<<itr->second+1;
 }
 else if(f)
 {itrr=sp.begin();
  cout<<*itrr<<" 0";
 }
 else
 cout<<"OK";
 return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool compare(const pair<int,int> &a,const pair<int,int> &b)
{if(a.first==b.first)
 return a.second<b.second;
 else
 return a.first<b.first;
}
int main()
{int n,m,i,flag=0,f=0;
 ios_base::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
 cin>>n>>m;
 vector< pair<int,int> > v(m);
 set<int> sp;
 map<int,int> mp;
 set<int>::iterator itrr;
 map<int,int>::iterator itr;
 for(i=0;i<m;++i)
 cin>>v[i].first>>v[i].second;
 sort(v.begin(),v.end(),compare);
 //cout<<"\n...\n";
 //for(i=0;i<m;++i)
 //cout<<v[i].first<<" "<<v[i].second<<"\n";

 for(i=0;i<m-1;++i)
 {if(v[i].second>=v[i+1].first)
  {++mp[v[i+1].first];
   //cout<<"abcd\n";
   flag=1;
  }
  else if(v[i+1].first>v[i].second+1)
  {f=1;
   sp.insert(v[i].second+1);
  }
 }

 //cout<<"\n...\n";
 //for(itr=mp.begin();itr!=mp.end();++itr)
 //cout<<itr->first<<" "<<itr->second<<"\n";
 //cout<<"\n...\n";
 //for(itrr=sp.begin();itrr!=sp.end();++itrr)
 //cout<<*itrr<<"\n";
 if(flag && f)
 {itr=mp.begin();
  itrr=sp.begin();
  if(itr->first<*itrr)
  cout<<itr->first<<" "<<itr->second+1;
  else
  cout<<*itrr<<" 0";
 }
 else if(flag)
 {itr=mp.begin();
  cout<<itr->first<<" "<<itr->second+1;
 }
 else if(f)
 {itrr=sp.begin();
  cout<<*itrr<<" 0";
 }
 else
 cout<<"OK";
 return 0;
}


//“Make it work, make it right, make it fast.” – Kent Beck
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
const int siz = 1e6 + 5;
const int MOD = 1e9 + 7;
#define endl '\n'
#define deb(x) cout << #x << " = " << x << endl;
//--------------------------------------------------------------------//
#define loop(i, a, n) for(ll i = a; i < n; i++)
#define rloop(i, a, n) for(ll i = n-1; i >= a; i--)
#define vi vector<int>
#define vl vector<ll>
#define vd vector<dd>
#define prii pair<int, int>
#define prll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
#define SORT(a,n) sort(begin(a),begin(a)+n)
#define all(x)  x.begin(), x.end()
#define endl '\n'
//--------------------------------------------------------------------//

void solve(){
	int n, m;
	cin >> n >> m;
	int count[101] = {0};
	for(int i = 0; i < m; i++){
		int a, b;
		cin >> a >> b;
		for(int j = a; j<=b; j++)
			++count[j];
	}
		for(int i = 1; i<=n; i++){
			if(count[i] != 1)
				{ cout << i << " " << count[i] << endl; return;}

		}
		cout << "OK\n";
}

int main(){
	#ifndef RAY
	//	freopen("input.txt", "r", stdin);
	#endif
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		int t = 1;
		//cin >> t;
		while(t--){
			solve();
		}
	return 0;
}


//*****SQUARE ROOT DECOMPOSITION*****
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pi 3.14159265  //35897932384626433
#define INF 1e18
#define MOD 1000000007
#define all(s) s.begin(),s.end()
#define mp make_pair
#define vc vector<int>
#define vp vector<pair<int,int> >
#define sec second
#define sz(x) (int)((x).size())
#define timesaver ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
const int N =  1e5 + 5;
ll gcd(ll a, ll b){ if(a==0) return b; else return gcd(b%a,a);}
ll power(ll base,ll exp,ll mod=MOD) {ll ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}

int k;
int a[N], bucval[N];
int getBucket(int i){
	return i / k;
}

int getStart(int i){
	return getBucket(i) * k;
}
int getEnd(int i, int n){
	return min(getStart(i) + k, n);
}

int main(){
	timesaver;
	int n, m, l, r;
	cin >> n >> m;
	k = sqrt(n);
	//cout << k << " ";
	while(m--){
		cin >> l >> r;
		l--;
		r--;
		if(getBucket(l) == getBucket(r)){
			for (int i = l; i <= r; i += 1)
			{
				a[i] += 1;
				//cout << i << " " << a[i] << "\n";
			}
		}
		else{
			for (int i = l; i < getEnd(l, n); i += 1)
			{
				//sum[getBucket(i)] += x;
				//cout <<"y" << " " << l << " " << getEnd(l, n) << "\n";
				a[i] += 1;
				//cout << i << " " << a[i] << "\n";
			}
			for (int i = getBucket(l) + 1; i < getBucket(r - 1); i += 1)
			{
				bucval[i] += 1;
				//sum[i] += l * k;
			}
			for (int i = getStart(r); i <= r; i += 1)
			{
				//sum[getBucket(i)] += x;
				a[i] += 1;
				//cout << i << " " << a[i] << "\n";
			}
		}
	}
	for (int i = 0; i < n; i += 1)
	{
		//cout <<" " << i << " " << a[i] << " " << bucval[getBucket(i)] << "\n";
		a[i] += bucval[getBucket(i)];
	}
	for (int i = 0; i < n; i += 1)
	{
		if(!a[i] || a[i] > 1)
			return cout << i + 1 << " " << a[i], 0;
	}
	cout << "OK";
}


//*****SQUARE ROOT DECOMPOSITION*****
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pi 3.14159265  //35897932384626433
#define INF 1e18
#define MOD 1000000007
#define all(s) s.begin(),s.end()
#define mp make_pair
#define vc vector<int>
#define vp vector<pair<int,int> >
#define sec second
#define sz(x) (int)((x).size())
#define timesaver ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
const int N =  1e5 + 5;
ll gcd(ll a, ll b){ if(a==0) return b; else return gcd(b%a,a);}
ll power(ll base,ll exp,ll mod=MOD) {ll ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}

int k;
int a[N], bucval[N];
int getBucket(int i){
	return i / k;
}

int getStart(int i){
	return getBucket(i) * k;
}
int getEnd(int i, int n){
	return min(getStart(i) + k, n);
}

int main(){
	timesaver;
	int n, m, l, r;
	cin >> n >> m;
	k = sqrt(n);
	//cout << k << " ";
	while(m--){
		cin >> l >> r;
		l--;
		r--;
		if(getBucket(l) == getBucket(r)){
			for (int i = l; i <= r; i += 1)
			{
				a[i] += 1;
				//cout << i << " " << a[i] << "\n";
			}
		}
		else{
			for (int i = l; i < getEnd(l, n); i += 1)
			{
				//sum[getBucket(i)] += x;
				//cout <<"y" << " " << l << " " << getEnd(l, n) << "\n";
				a[i] += 1;
				//cout << i << " " << a[i] << "\n";
			}
			for (int i = getBucket(l) + 1; i < getBucket(r); i += 1)
			{
				bucval[i] += 1;
				//sum[i] += l * k;
			}
			for (int i = getStart(r); i <= r; i += 1)
			{
				//sum[getBucket(i)] += x;
				a[i] += 1;
				//cout << i << " " << a[i] << "\n";
			}
		}
	}
	for (int i = 0; i < n; i += 1)
	{
		//cout <<" " << i << " " << a[i] << " " << bucval[getBucket(i)] << "\n";
		a[i] += bucval[getBucket(i)];
	}
	for (int i = 0; i < n; i += 1)
	{
		if(!a[i] || a[i] > 1)
			return cout << i + 1 << " " << a[i], 0;
	}
	cout << "OK";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    map<int,int> mp;
    int n,m;
    cin>>n>>m;
    int u,v;
    int i,j;
    for(i=0;i<m;i++)
    {
        cin>>u>>v;
        for(j=u;j<=v;j++)
        {
            mp[j]++;
        }
    }
     map<int,int>::iterator it;
     for(it=mp.begin();it!=mp.end();it++)
     {
         if(mp[it->first]==0||mp[it->first]>1)
         {
             cout<<it->first<<" "<<mp[it->first]<<endl;
             return 0;
         }
     }
     cout<<"OK"<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    map<int,int> mp;
    int n,m;
    cin>>n>>m;
    int u,v;
    int i,j;
    for(i=0;i<m;i++)
    {
        cin>>u>>v;
        for(j=u;j<=v;j++)
        {
            mp[j]++;
        }
    }
     map<int,int>::iterator it;
     for(it=mp.begin();it!=mp.end();it++)
     {
         if(mp[it->first]==0||mp[it->first]>1)
         {
             cout<<it->first<<" "<<mp[it->first]<<endl;
         }
     }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    map<int,int> mp;
    int n,m;
    cin>>n>>m;
    int u,v;
    int i,j;
    for(i=0;i<m;i++)
    {
        cin>>u>>v;
        for(j=u;j<=v;j++)
        {
            mp[j]++;
        }
    }
     i=1;
     for(i=1;i<=n;i++)
     {
         if(mp[i]==0||mp[i]>1)
         {
             cout<<i<<" "<<mp[i]<<endl;
             return 0;
         }
     }
     cout<<"OK"<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    map<int,int> mp;
    int n,m;
    cin>>n>>m;
    int u,v;
    for(i=0;i<m;i++)
    {
        cin>>u>>v;
        for(j=u;j<=v;j++)
        {
            mp[j]++;
        }
    }
     map<int,int>::iterator it;
     for(it=mp.begin();it!=mp.end();it++)
     {
         if(mp[it->first]==0||mp[it->first]>1)
         {
             cout<<it->first<<" "<<mp[it->first]<<endl;
         }
     }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define tc int t; cin>>t; while(t--)
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mod 1000000007
#define pb push_back
#define vi vector<int>
#define vc vector<char>
#define eb emplace_back
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fo(i,x,n) for(int i=x;i<n;i++)
#define csb(x) __builtin_popcount(x)
#define tz(x) __builtin_ctz(x)
#define lsbp(x) __builtin_ffs(x)
#define msbp(x) __lg(x)
#define btoi(str,i) stoi(str,0,i)
#define ff first
#define ss second
const int inf=0x3f3f3f3f3f3f3f3f;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void zz(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

int32_t main(){
zz();
int n,m;cin>>n>>m;
int x,y;
vi cnt(101,0);
vi b(101,0);
for(int i=0;i<m;i++){
   cin>>x>>y;
   if(x==y)b[x]=1;
   for(int i=x;i<=y;i++){
      cnt[i]+=1;
   }
}
// cout<<cnt[1]<<" ";
// for(int i=1;i<=100;i++)cout<<b[i]<<" ";
for(int i=1;i<=n;i++){
   // cout<<"h";
   if(!cnt[i] || cnt[i]>2){

      cout<<i<<" "<<cnt[i]<<endl;
      return 0;
   }
    if(cnt[i]==2){
      if(!b[i]){
         cout<<i<<" "<<cnt[i]<<endl;
         return 0;
      }
   }
}
cout<<"OK"<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define boost() ios_base :: sync_with_stdio(0); cin.tie(); cout.tie(); cout << fixed; cout << setprecision(15); srand(time(NULL))
#define endl '\n'
typedef long long ll;
const int _N = 1e6 + 11;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

int n, a[_N], b[_N], mx_tree[_N * 4], mn_tree[_N * 4];
ll res = 0;

void pre (int p, int l, int r) {
	if (l == r) {
		mx_tree[p] = a[l];
		mn_tree[p] = b[l];
	} else {
		int md = (l + r) >> 1;
		pre(2 * p, l, md);
		pre(2 * p + 1, md + 1, r);
		mx_tree[p] = max(mx_tree[2 * p], mx_tree[2 * p + 1]);
		mn_tree[p] = min(mn_tree[2 * p], mn_tree[2 * p + 1]);
	}
}

pair <int, int> get(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return {INT_MIN, INT_MAX};
	if (l >= L && r <= R) return {mx_tree[p], mn_tree[p]};
	int md = (l + r) >> 1;
	pair <int, int> v1 = get(2 * p, l, md, L, R);
	pair <int, int> v2 = get(2 * p + 1, md + 1, r, L, R);
	return {max(v1.first, v2.first), min(v1.second, v2.second)};
}

int main() {
	boost();
	#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
	#endif
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++) cin >> b[i];
	pre(1, 0, n - 1);
	for (int i = 0; i < n; i++) {
		int lo = i - 1, hi = n;
		while (lo + 1 < hi) {
			int md = (lo + hi) >> 1;
			pair <int, int> mmm = get(1, 0, n - 1, i, md);
			int mx = mmm.first;
			int mn = mmm.second;
			if (mx <= mn) lo = md;
			else hi = md;
		}
		res += lo - i + 1;
		hi = lo + 1;
		lo = i - 1;
		while (lo + 1 < hi) {
			int md = (lo + hi) >> 1;
			pair <int, int> mmm = get(1, 0, n - 1, i, md);
			int mx = mmm.first;
			int mn = mmm.second;
			if (mx < mn) lo = md;
			else hi = md;
		}
		res -= (hi - i + 1);
		res += 1;
	}
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define boost() ios_base :: sync_with_stdio(0); cin.tie(); cout.tie(); cout << fixed; cout << setprecision(15); srand(time(NULL))
#define endl '\n'
typedef long long ll;
const int _N = 2e5 + 11;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll n, a[_N], b[_N], mx_tree[_N * 4], mn_tree[_N * 4], res = 0;

void pre (int p, int l, int r) {
	if (l == r) {
		mx_tree[p] = a[l];
		mn_tree[p] = b[l];
	} else {
		int md = (l + r) >> 1;
		pre(2 * p, l, md);
		pre(2 * p + 1, md + 1, r);
		mx_tree[p] = max(mx_tree[2 * p], mx_tree[2 * p + 1]);
		mn_tree[p] = min(mn_tree[2 * p], mn_tree[2 * p + 1]);
	}
}

int get_max(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MIN;
	if (l >= L && r <= R) return mx_tree[p];
	int md = (l + r) >> 1;
	return max(get_max(2 * p, l, md, L, R), get_max(2 * p + 1, md + 1, r, L, R));
}

int get_min(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MAX;
	if (l >= L && r <= R) return mn_tree[p];
	int md = (l + r) >> 1;
	return min(get_min(2 * p, l, md, L, R), get_min(2 * p + 1, md + 1, r, L, R));
}

int main() {
//	boost();
	#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
	#endif
//	cin >> n;
	scanf("%lld", &n);
	for (int i = 0; i < n; i++) scanf("%lld", a + i); //cin >> a[i];
	for (int i = 0; i < n; i++) scanf("%lld", b + i); //cin >> b[i];
	pre(1, 0, n - 1);
	for (int i = 0; i < n; i++) {
		int lo = i - 1, hi = n;
		while (lo + 1 < hi) {
			int md = (lo + hi) >> 1;
			int mx = get_max(1, 0, n - 1, i, md);
			int mn = get_min(1, 0, n - 1, i, md);
			if (mx <= mn) lo = md;
			else hi = md;
		}
		res += lo - i + 1;
		hi = lo + 1;
		lo = i - 1;
		while (lo + 1 < hi) {
			int md = (lo + hi) >> 1;
			int mx = get_max(1, 0, n - 1, i, md);
			int mn = get_min(1, 0, n - 1, i, md);
			if (mx < mn) lo = md;
			else hi = md;
		}
		res -= (hi - i + 1);
		res += 1;
	}
//	cout << res << endl;
	printf("%lld\n", res);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define boost() ios_base :: sync_with_stdio(0); cin.tie(); cout.tie(); cout << fixed; cout << setprecision(15); srand(time(NULL))
#define endl '\n'
typedef long long ll;
const int _N = 1e6 + 11;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll n, a[_N], b[_N], mx_tree[_N * 4], mn_tree[_N * 4], res = 0;

void pre (int p, int l, int r) {
	if (l == r) {
		mx_tree[p] = a[l];
		mn_tree[p] = b[l];
	} else {
		int md = (l + r) >> 1;
		pre(2 * p, l, md);
		pre(2 * p + 1, md + 1, r);
		mx_tree[p] = max(mx_tree[2 * p], mx_tree[2 * p + 1]);
		mn_tree[p] = min(mn_tree[2 * p], mn_tree[2 * p + 1]);
	}
}

int get_max(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MIN;
	if (l >= L && r <= R) return mx_tree[p];
	int md = (l + r) >> 1;
	return max(get_max(2 * p, l, md, L, R), get_max(2 * p + 1, md + 1, r, L, R));
}

int get_min(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MAX;
	if (l >= L && r <= R) return mn_tree[p];
	int md = (l + r) >> 1;
	return min(get_min(2 * p, l, md, L, R), get_min(2 * p + 1, md + 1, r, L, R));
}

int main() {
	boost();
	#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
	#endif
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++) cin >> b[i];
	pre(1, 0, n - 1);
	for (int i = 0; i < n; i++) {
		int lo = i - 1, hi = n;
		while (lo + 1 < hi) {
			int md = (lo + hi) >> 1;
			int mx = get_max(1, 0, n - 1, i, md);
			int mn = get_min(1, 0, n - 1, i, md);
			if (mx <= mn) lo = md;
			else hi = md;
		}
		res += lo - i + 1;
		hi = lo + 1;
		lo = i - 1;
		while (lo + 1 < hi) {
			int md = (lo + hi) >> 1;
			int mx = get_max(1, 0, n - 1, i, md);
			int mn = get_min(1, 0, n - 1, i, md);
			if (mx < mn) lo = md;
			else hi = md;
		}
		res -= (hi - i + 1);
		res += 1;
	}
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define boost() ios_base :: sync_with_stdio(0); cin.tie(); cout.tie(); cout << fixed; cout << setprecision(15); srand(time(NULL))
#define endl '\n'
typedef long long LL;
const int _N = 2e5 + 11;
const LL INF = 1e18;
const LL MOD = 1e9 + 7;

LL n, res = 0, a[_N], b[_N], a_tree[2][_N * 4], b_tree[2][_N * 4];
map <LL, vector <LL>> m;

void bld(int p, int l, int r) {
	if (l == r) {
		a_tree[0][p] = a[l];
		a_tree[1][p] = a[l];
		b_tree[0][p] = b[l];
		b_tree[1][p] = b[l];
	} else {
		int mid = (l + r) >> 1;
		bld(2 * p, l, mid);
		bld(2 * p + 1, mid + 1, r);
		a_tree[0][p] = min(a_tree[0][2*p], a_tree[0][2*p+1]);
		a_tree[1][p] = max(a_tree[1][2*p], a_tree[1][2*p+1]);
		b_tree[0][p] = min(b_tree[0][2*p], b_tree[0][2*p+1]);
		b_tree[1][p] = max(b_tree[1][2*p], b_tree[1][2*p+1]);
	}
}

int get_max_a(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MIN;
	if (l >= L && r <= R) return a_tree[1][p];
	int mid = (l + r) >> 1;
	return max(get_max_a(2 * p, l, mid, L, R), get_max_a(2 * p + 1, mid + 1, r, L, R));
}

int get_min_a(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MAX;
	if (l >= L && r <= R) return a_tree[0][p];
	int mid = (l + r) >> 1;
	return min(get_min_a(2 * p, l, mid, L, R), get_min_a(2 * p + 1, mid + 1, r, L, R));
}

int get_max_b(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MIN;
	if (l >= L && r <= R) return b_tree[1][p];
	int mid = (l + r) >> 1;
	return max(get_max_b(2 * p, l, mid, L, R), get_max_b(2 * p + 1, mid + 1, r, L, R));
}

int get_min_b(int p, int l, int r, int L, int R) {
	if (l > r || l > R || r < L) return INT_MAX;
	if (l >= L && r <= R) return b_tree[0][p];
	int mid = (l + r) >> 1;
	return min(get_min_b(2 * p, l, mid, L, R), get_min_b(2 * p + 1, mid + 1, r, L, R));
}

int get_hi_a(int idx) {
	int lo = idx - 1;
	int hi = n;
	while (lo + 1 < hi) {
		int mid = (lo + hi) >> 1;
		if (get_max_a(1, 0, n - 1, idx, mid) <= a[idx]) lo = mid;
		else hi = mid;
	}
	return lo;
}

int get_hi_b(int idx) {
	int lo = idx - 1;
	int hi = n;
	while (lo + 1 < hi) {
		int mid = (lo + hi) >> 1;
		if (get_min_b(1, 0, n - 1, idx, mid) < a[idx]) hi = mid;
		else lo = mid;
	}
	return lo;
}

int main() {
	boost();
	#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
//		freopen("out", "w", stdout);
	#endif
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		m[b[i]].pb(i);
	}
	bld(1, 0, n - 1);
	for (int i = 0; i < n; i++) {
		int hi = (get_hi_b(i), get_hi_a(i));
		if (hi < i) continue;
		auto it = lower_bound(all(m[a[i]]), i);
		if (it == m[a[i]].end() || *it > hi) continue;
		res += hi - *it + 1;
	}
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define ll long long
#define pb push_back
#define ppb pop_back
#define mod 1000000007
#define mx 200011
#define inf 1000000009
#define ff first
#define ss second
using namespace std;
int n,m;
int treemn[5*mx],treemx[5*mx];
void build() {
    for (int i = n - 1; i > 0; --i){
        treemx[i] = max( treemx[i<<1] ,treemx[i<<1|1] );
        treemn[i] = min( treemn[i<<1] ,treemn[i<<1|1] );
    }
}
int querymn(int l,int r){
    int res1=INT_MAX;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = min( res1 , treemn[l++] );
        }
        if (r&1) {
            res1 = min( res1 , treemn[--r] );
        }
    }
    return res1;
}
int querymx(int l,int r){
    int res1=-inf;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = max( res1 , treemx[l++] );
        }
        if (r&1) {
            res1 = max( res1 , treemx[--r] );
        }
    }
    return res1;
}
int bin(int l,int r){
    int upper,lower,low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)>0 ){
            high=mid;
        }
        else{
            low = mid;
        }
    }
    for(int i=high;i>=low;i--){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            upper=i;
            break;
        }
        if(i==low){
            upper=i;
        }
    }
    low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)<0 ){
            low=mid;
        }
        else{
            high = mid;
        }
    }
    for(int i=low;i<=high;i++){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            lower=i;
            break;
        }
        if(i==high){
            lower=i;
        }
    }
    return max( upper-lower+1 , 0 );
}
int  main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",treemx+n+i);
    }
    for(int i=0;i<n;i++){
        scanf("%d",treemn+n+i);
    }
    build();
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans += bin(i,n);
    }
    cout<<ans-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define ll long long
#define pb push_back
#define ppb pop_back
#define mod 1000000007
#define mx 200011
#define inf 1000000009
#define ff first
#define ss second
using namespace std;
int n,m;
int treemn[5*mx],treemx[5*mx];
void build() {
    for (int i = n - 1; i > 0; --i){
        treemx[i] = max( treemx[i<<1] ,treemx[i<<1|1] );
        treemn[i] = min( treemn[i<<1] ,treemn[i<<1|1] );
    }
}
int querymn(int l,int r){
    int res1=INT_MAX;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = min( res1 , treemn[l++] );
        }
        if (r&1) {
            res1 = min( res1 , treemn[--r] );
        }
    }
    return res1;
}
int querymx(int l,int r){
    int res1=-inf;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = max( res1 , treemx[l++] );
        }
        if (r&1) {
            res1 = max( res1 , treemx[--r] );
        }
    }
    return res1;
}
int bin(int l,int r){
    int upper,lower,low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)>0 ){
            high=mid;
        }
        else{
            low = mid;
        }
    }
    for(int i=high;i>=low;i--){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            upper=i;
            break;
        }
        if(i==low){
            upper=i;
        }
    }
    low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)<0 ){
            low=mid;
        }
        else{
            high = mid;
        }
    }
    for(int i=low;i<=high;i++){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            lower=i;
            break;
        }
        if(i==high){
            lower=i;
        }
    }
    if(upper==lower){
        if( treemx[n+upper-1]==treemn[n+lower-1] )return 1;
        else return 0;
    }
    return max( upper-lower+1 , 0 );
}
int  main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",treemx+n+i);
    }
    for(int i=0;i<n;i++){
        scanf("%d",treemn+n+i);
    }
    build();
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans += bin(i,n);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define ll long long
#define pb push_back
#define ppb pop_back
#define mod 1000000007
#define mx 200011
#define inf 1000000009
#define ff first
#define ss second
using namespace std;
int n,m;
int treemn[5*mx],treemx[5*mx];
void build() {
    for (int i = n - 1; i > 0; --i){
        treemx[i] = max( treemx[i<<1] ,treemx[i<<1|1] );
        treemn[i] = min( treemn[i<<1] ,treemn[i<<1|1] );
    }
}
int querymn(int l,int r){
    int res1=INT_MAX;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = min( res1 , treemn[l++] );
        }
        if (r&1) {
            res1 = min( res1 , treemn[--r] );
        }
    }
    return res1;
}
int querymx(int l,int r){
    int res1=-inf;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = max( res1 , treemx[l++] );
        }
        if (r&1) {
            res1 = max( res1 , treemx[--r] );
        }
    }
    return res1;
}
int bin(int l,int r){
    int upper,lower,low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)>0 ){
            high=mid;
        }
        else{
            low = mid;
        }
    }
    for(int i=high;i>=low;i--){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            upper=i;
            break;
        }
        if(i==low){
            upper=i;
        }
    }
    low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)<0 ){
            low=mid;
        }
        else{
            high = mid;
        }
    }
    for(int i=low;i<=high;i++){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            lower=i;
            break;
        }
        if(i==high){
            lower=i;
        }
    }
    if(upper==lower && l==r){
        if( treemx[n+upper-1]==treemn[n+lower-1] )return 1;
        else return 0;
    }
    return max( upper-lower+1 , 0 );
}
int  main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",treemx+n+i);
    }
    for(int i=0;i<n;i++){
        scanf("%d",treemn+n+i);
    }
    build();
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans += bin(i,n);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define ll long long
#define pb push_back
#define ppb pop_back
#define mod 1000000007
#define mx 200011
#define inf 1e9+9
#define ff first
#define ss second
using namespace std;
int n,m;
int arr[mx],arr1[mx],treemn[4*mx],treemx[4*mx];
void build(int nd,int b,int e){
    if(b==e){
        treemx[nd] = arr[b];
        treemn[nd] = arr1[b];
        return;
    }
    int mid=(b+e)>>1;
    int l  =nd<<1;
    int r  =l+1;
    build(l,b,mid);
    build(r,mid+1,e);
    treemx[nd] = max(treemx[l],treemx[r]);
    treemn[nd] = min(treemn[l],treemn[r]);
    return;
}
int querymx(int nd,int b,int e,int i,int j){
    if(e<i||b>j)return -inf;
    if(b>=i && e<=j)return treemx[nd];
    int mid=(b+e)>>1;
    return max( querymx(nd<<1,b,mid,i,j) , querymx(nd<<1|1,mid+1,e,i,j) );
}
int querymn(int nd,int b,int e,int i,int j){
    if(e<i||b>j)return inf;
    if(b>=i && e<=j)return treemn[nd];
    int mid=(b+e)>>1;
    return min( querymn(nd<<1,b,mid,i,j) , querymn(nd<<1|1,mid+1,e,i,j) );
}
int bin(int l,int r){
    int upper,lower,low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(1,1,n,l,mid)-querymn(1,1,n,l,mid)>0 ){
            high=mid;
        }
        else{
            low = mid;
        }
    }
    for(int i=high;i>=low;i--){
        if( querymx(1,1,n,l,i)==querymn(1,1,n,l,i) ){
            upper=i;
            break;
        }
        if(i==low){
            upper=i;
        }
    }
    low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(1,1,n,l,mid)-querymn(1,1,n,l,mid)<0 ){
            low=mid;
        }
        else{
            high = mid;
        }
    }
    for(int i=low;i<=high;i++){
        if( querymx(1,1,n,l,i)==querymn(1,1,n,l,i) ){
            lower=i;
            break;
        }
        if(i==high){
            lower=i;
        }
    }
    return max( upper-lower+1 , 0 );
}
int  main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&arr[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&arr1[i]);
    }
    build(1,1,n);
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans += bin(i,n);
    }
    cout<<ans-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define ll long long
#define pb push_back
#define ppb pop_back
#define mod 1000000007
#define mx 200011
#define inf 1e9+9
#define ff first
#define ss second
using namespace std;
int n,m;
int arr[mx],arr1[mx],treemn[4*mx],treemx[4*mx];
void build() {
    for (int i = n - 1; i > 0; --i){
        treemx[i] = max( treemx[i<<1] ,treemx[i<<1|1] );
        treemn[i] = min( treemn[i<<1] ,treemn[i<<1|1] );
    }
}
int querymn(int l,int r){
    int res1=INT_MAX;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = min( res1 , treemn[l++] );
        }
        if (r&1) {
            res1 = min( res1 , treemn[--r] );
        }
    }
    return res1;
}
int querymx(int l,int r){
    int res1=-inf;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            res1 = max( res1 , treemx[l++] );
        }
        if (r&1) {
            res1 = max( res1 , treemx[--r] );
        }
    }
    return res1;
}
int bin(int l,int r){
    int upper,lower,low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)>0 ){
            high=mid;
        }
        else{
            low = mid;
        }
    }
    for(int i=high;i>=low;i--){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            upper=i;
            break;
        }
        if(i==low){
            upper=i;
        }
    }
    low=l,high=r;
    while(high-low>2){
        int mid=(low+high)>>1;
        if( querymx(l-1,mid)-querymn(l-1,mid)<0 ){
            low=mid;
        }
        else{
            high = mid;
        }
    }
    for(int i=low;i<=high;i++){
        if( querymx(l-1,i)==querymn(l-1,i) ){
            lower=i;
            break;
        }
        if(i==high){
            lower=i;
        }
    }
    return max( upper-lower+1 , 0 );
}
int  main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",treemx+n+i);
    }
    for(int i=0;i<n;i++){
        scanf("%d",treemn+n+i);
    }
    build();
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans += bin(i,n);
    }
    cout<<ans-1<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000];
int t2[20][400000];
int logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
    for (int i = 0; i < n; ++i) cin >> table[0][i];
    for (int sz = 1; sz <= logTable[n]; ++sz) {
      for (int i = 0; i < n; ++i) {
        table[sz][i] = table[sz - 1][i];
        if (i + (1 << (sz - 1)) < n) {
          table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
        }
      }
    }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
    int k = logTable[j - i + 1];
    return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int lwr1 = i, upr1 = n - 1;
    while (lwr1 < upr1) {
      int mid = lwr1 + (upr1 - lwr1) / 2;
      getQuery(t1, i, mid, maxFu) >= getQuery(t2, i, mid, minFu) ? upr1 = mid : lwr1 = mid + 1;
    }
    int lwr2 = i, upr2 = n - 1;
    while (lwr2 < upr2) {
      int mid = lwr2 + (upr2 - lwr2 + 1) / 2;
      getQuery(t1, i, mid, maxFu) <= getQuery(t2, i, mid, minFu) ? lwr2 = mid : upr2 = mid - 1;
    }
    if (getQuery(t1, i, lwr1, maxFu) == getQuery(t2, i, lwr1, minFu)) {
      ans += lwr2 - lwr1 + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, lwr = 0, upr = 0; i < n; ++i, lwr = max(lwr, i), upr = max(upr, i)) {
    while (lwr < n && getQuery(t1, i, lwr, maxFu) < getQuery(t2, i, lwr, minFu)) ++lwr, ++upr;
    while (upr < n && getQuery(t1, i, upr, maxFu) <= getQuery(t2, i, upr, minFu)) ++upr;
    if (getQuery(t1, i, lwr, maxFu) == getQuery(t2, i, lwr, minFu)) {
      ans += upr - lwr;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, curLwr = 0; i < n; ++i, curLwr = max(curLwr, i)) {
    while (curLwr < n && getQuery(t1, i, curLwr, maxFu) < getQuery(t2, i, curLwr, minFu)) ++curLwr;
    if (getQuery(t1, i, curLwr, maxFu) == getQuery(t2, i, curLwr, minFu)) {
      int lwr = curLwr, upr = n - 1;
      while (lwr < upr) {
        int mid = lwr + (upr - lwr + 1) / 2;
        getQuery(t1, i, mid, maxFu) <= getQuery(t2, i, mid, minFu) ? lwr = mid : upr = mid - 1;
      }
      ans += lwr - curLwr + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000];
int t2[20][400000];
int logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; ++i) {
    logTable[i] = 1 + logTable[i >> 1];
  }
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
    for (int i = 0; i < n; ++i) cin >> table[0][i];
    for (int sz = 1; sz <= logTable[n]; ++sz) {
      for (int i = 0; i < n; ++i) {
        table[sz][i] = table[sz - 1][i];
        if (i + (1 << (sz - 1)) < n) {
          table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
        }
      }
    }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
    int k = logTable[j - i + 1];
    return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int lwr1 = i, upr1 = n - 1;
    while (lwr1 < upr1) {
      int mid = lwr1 + (upr1 - lwr1) / 2;
      if (getQuery(t1, i, mid, maxFu) >= getQuery(t2, i, mid, minFu)) {
        upr1 = mid;
      } else {
        lwr1 = mid + 1;
      }
    }
    int lwr2 = i, upr2 = n - 1;
    while (lwr2 < upr2) {
      int mid = lwr2 + (upr2 - lwr2 + 1) / 2;
      if (getQuery(t1, i, mid, maxFu) <= getQuery(t2, i, mid, minFu)) {
        lwr2 = mid;
      } else {
        upr2 = mid - 1;
      }
    }
    if (getQuery(t1, i, lwr1, maxFu) == getQuery(t2, i, lwr1, minFu)) {
      ans += lwr2 - lwr1 + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
    for (int i = 0; i < n; ++i) cin >> table[0][i];
    for (int sz = 1; sz <= logTable[n]; ++sz) {
      for (int i = 0; i < n; ++i) {
        table[sz][i] = table[sz - 1][i];
        if (i + (1 << (sz - 1)) < n) {
          table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
        }
      }
    }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
    int k = logTable[j - i + 1];
    return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int lwr1 = i, upr1 = n - 1;
    while (lwr1 < upr1) {
      int mid = lwr1 + (upr1 - lwr1) / 2;
      getQuery(t1, i, mid, maxFu) >= getQuery(t2, i, mid, minFu) ? upr1 = mid : lwr1 = mid + 1;
    }
    int lwr2 = i, upr2 = n - 1;
    while (lwr2 < upr2) {
      int mid = lwr2 + (upr2 - lwr2 + 1) / 2;
      getQuery(t1, i, mid, maxFu) <= getQuery(t2, i, mid, minFu) ? lwr2 = mid : upr2 = mid - 1;
    }
    if (getQuery(t1, i, lwr1, maxFu) == getQuery(t2, i, lwr1, minFu)) {
      ans += lwr2 - lwr1 + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, lwr = 0, upr = 0; i < n; ++i, lwr = max(lwr, i), upr = max(upr, i)) {
    while (lwr < n && getQuery(t1, i, lwr, maxFu) < getQuery(t2, i, lwr, minFu)) ++lwr;
    while (upr < n && getQuery(t1, i, upr, maxFu) <= getQuery(t2, i, upr, minFu)) ++upr;
    if (t1[0][i] < t2[0][i]) {
      ans += upr - lwr;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, curLwr = 0; i < n; ++i, curLwr = max(curLwr, i)) {
    while (curLwr < n && getQuery(t1, i, curLwr, maxFu) < getQuery(t2, i, curLwr, minFu)) ++curLwr;
    if (getQuery(t1, i, curLwr, maxFu) == getQuery(t2, i, curLwr, minFu)) {
      int lwr = curLwr, upr = n - 1;
      while (lwr < upr) {
        int mid = lwr + (upr - lwr + 1) / 2;
        getQuery(t1, curLwr, mid, maxFu) <= getQuery(t2, curLwr, mid, minFu) ? lwr = mid : upr = mid - 1;
      }
      ans += lwr - curLwr + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000];
int t2[20][400000];
int logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; ++i) {
    logTable[i] = 1 + logTable[i >> 1];
  }
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [](int table[][400000], int n, int(m)(int, int)) {
    for (int i = 0; i < n; ++i) cin >> table[0][i];
    for (int sz = 1; sz <= logTable[n]; ++sz) {
      for (int i = 0; i < n; ++i) {
        table[sz][i] = table[sz - 1][i];
        if (i + (1 << (sz - 1)) < n) {
          table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
        }
      }
    }
  };
  auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
    int k = logTable[j - i + 1];
    return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, n, maxFu);
  readAndBuild(t2, n, minFu);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int lwr1 = i, upr1 = n - 1;
    while (lwr1 < upr1) {
      int mid = lwr1 + (upr1 - lwr1) / 2;
      if (getQuery(t1, i, mid, maxFu) >= getQuery(t2, i, mid, minFu)) {
        upr1 = mid;
      } else {
        lwr1 = mid + 1;
      }
    }
    int lwr2 = i, upr2 = n - 1;
    while (lwr2 < upr2) {
      int mid = lwr2 + (upr2 - lwr2 + 1) / 2;
      if (getQuery(t1, i, mid, maxFu) <= getQuery(t2, i, mid, minFu)) {
        lwr2 = mid;
      } else {
        upr2 = mid - 1;
      }
    }
    if (getQuery(t1, i, lwr1, maxFu) == getQuery(t2, i, lwr1, minFu)) {
      ans += lwr2 - lwr1 + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, curLwr = 0; i < n; ++i, curLwr = max(curLwr, i)) {
    while (curLwr < n && getQuery(t1, i, curLwr, maxFu) < getQuery(t2, i, curLwr, minFu)) ++curLwr;
    if (getQuery(t1, i, curLwr, maxFu) == getQuery(t2, i, curLwr, minFu)) {
      int lwr = curLwr, upr = n - 1;
      while (lwr < upr) {
        int mid = lwr + (upr - lwr + 1) / 2;
        getQuery(t1, lwr, mid, maxFu) <= getQuery(t2, lwr, mid, minFu) ? lwr = mid : upr = mid - 1;
      }
      ans += lwr - curLwr + 1;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, lwr = 0, upr = 0; i < n; ++i, lwr = max(lwr, i), upr = max(upr, i)) {
    while (lwr < n && getQuery(t1, i, lwr, maxFu) < getQuery(t2, i, lwr, minFu)) ++lwr;
    while (upr < n && getQuery(t1, i, upr, maxFu) <= getQuery(t2, i, upr, minFu)) ++upr;
    if (t1[0][i] <= t2[0][i]) {
      ans += upr - lwr;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t1[20][400000], t2[20][400000], logTable[400000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 2; i <= n; logTable[i] = 1 + logTable[i >> 1], ++i);
  const auto maxFu = [](int a, int b) { return a > b ? a : b; };
  const auto minFu = [](int a, int b) { return a < b ? a : b; };
  const auto readAndBuild = [n](int table[][400000], int(m)(int, int)) {
      for (int i = 0; i < n; ++i) cin >> table[0][i];
      for (int sz = 1; sz <= logTable[n]; ++sz) {
        for (int i = 0; i < n; ++i) {
          table[sz][i] = table[sz - 1][i];
          if (i + (1 << (sz - 1)) < n) {
            table[sz][i] = m(table[sz][i], table[sz - 1][i + (1 << (sz - 1))]);
          }
        }
      }
  };
  const auto getQuery = [](int table[][400000], int i, int j, int m(int, int)) {
      int k = logTable[j - i + 1];
      return m(table[k][i], table[k][j - (1 << k) + 1]);
  };
  readAndBuild(t1, maxFu);
  readAndBuild(t2, minFu);
  long long ans = 0;
  for (int i = 0, lwr = 0, upr = 0; i < n; ++i, lwr = max(lwr, i), upr = max(upr, i)) {
    while (lwr < n && getQuery(t1, i, lwr, maxFu) < getQuery(t2, i, lwr, minFu)) ++lwr;
    while (upr < n && getQuery(t1, i, upr, maxFu) <= getQuery(t2, i, upr, minFu)) ++upr;
    if (getQuery(t1, i, lwr, maxFu) == getQuery(t2, i, lwr, minFu)) {
      ans += upr - lwr;
    }
  }
  cout << ans << '\n';

  return 0;
}


#include<bits/stdc++.h>
#define N 200005
#define int long long
#define pb push_back
using namespace std;
int n, a[25][N], b[25][N], Log[N], ans;
inline get1(int l, int r)
{
    int k = Log[r - l];
    return max(a[k][l], a[k][r - (1 << k)]);
}
inline get2(int l, int r)
{
    int k = Log[r - l];
    return min(b[k][l], b[k][r - (1 << k)]);
}
signed main()
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[0][i];
    for (int i = 0; i < n; i++) cin >> b[0][i];
    Log[0] = Log[1] = 0;
    for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;
    for (int k = 1; k < 18; k++)
        for (int i = 0; i + (1 << k) <= n; i++)
        {
            a[k][i] = max(a[k - 1][i], a[k - 1][i + (1 << k - 1)]);
            b[k][i] = min(b[k - 1][i], b[k - 1][i + (1 << k - 1)]);
        }
    for (int i = 0; i < n; i++)
    {
        int l = i, r = n + 1, A = i;
        while (r - l > 1)
        {
            int mid = (l + r) / 2;
            if (get1(i, mid) <= get2(i, mid))
                l = mid;
            else r = mid;
        }
        A = r;
        l = i, r = n + 1;
        while (r - l > 1)
        {
            int mid = (l + r) / 2;
            if (get1(i, mid) < get2(i, mid))
                l = mid;
            else r = mid;
        }
        ans += A - r;
    }
    cout << ans;
}


#include<bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int n, m, a[N][25], b[N][25], Log[N], ans;
int getA(int l, int r) {
    int j = Log[r - l + 1];
    return max(a[l][j], a[r - (1 << j)][j]);
}
int getB(int l, int r) {
    int j = Log[r - l + 1];
    return min(b[l][j], b[r - (1 << j)][j]);
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i][0];
    for (int i = 1; i <= n; i++) cin >> b[i][0];
    for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;
    for (int j = 1; j < 18; j++)
        for (int i = 1; i + (1 << j) <= n; i++) {
            a[i][j] = max(a[i][j - 1], a[i + (1 << j - 1)][j - 1]);
            b[i][j] = min(b[i][j - 1], b[i + (1 << j - 1)][j - 1]);
        }
    for (int i = 1; i <= n; i++)
    {
        int lo = i, hi = n, mi, L = -1, R = -1;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                L = mi;
                hi = mi - 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (L == -1) continue;
        lo = 1, hi = n;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                R = mi;
                lo = mi + 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        ans += (R - L + 1);
    }
    cout << ans;
}


#include<bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int n, m, a[N][25], b[N][25], Log[N], ans;
int getA(int l, int r) {
    int j = Log[r - l + 1];
    //cout << j << ' ' << a[l][j] << '\n';
    return max(a[l][j], a[r - (1 << j) + 1][j]);
}
int getB(int l, int r) {
    int j = Log[r - l + 1];
    //cout << j << ' ' << b[l][j] << '\n';
    return min(b[l][j], b[r - (1 << j) + 1][j]);
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i][0];
    for (int i = 0; i < n; i++) cin >> b[i][0];
    Log[0] = Log[1] = 0;
    for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;
    for (int j = 1; j < 18; j++)
        for (int i = 0; i + (1 << j) <= n; i++) {
            a[i][j] = max(a[i][j - 1], a[i + (1 << j - 1)][j - 1]);
            b[i][j] = min(b[i][j - 1], b[i + (1 << j - 1)][j - 1]);
        }
    for (int i = 0; i < n; i++)
    {
        int lo = i, hi = n, mi, L = -1, R = -1;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                L = mi;
                hi = mi - 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (L == -1) continue;
        lo = i, hi = n;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                R = mi;
                lo = mi + 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (R == -1) continue;
        ans += (R - L + 1);
    }
    //cout << getA(5, 6) << ' ' << getB(5, 6) << '\n';
    cout << ans;
}


#include<bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int n, m, a[N][25], b[N][25], Log[N], ans;
int getA(int l, int r) {
    int j = Log[r - l + 1];
    //cout << j << ' ' << a[l][j] << '\n';
    return max(a[l][j], a[r - (1 << j) + 1][j]);
}
int getB(int l, int r) {
    int j = Log[r - l + 1];
    //cout << j << ' ' << b[l][j] << '\n';
    return min(b[l][j], b[r - (1 << j) + 1][j]);
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i][0];
    for (int i = 0; i < n; i++) cin >> b[i][0];
    Log[0] = Log[1] = 0;
    for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;
    for (int j = 1; j < 18; j++)
        for (int i = 0; i + (1 << j) <= n; i++) {
            a[i][j] = max(a[i][j - 1], a[i + (1 << j - 1)][j - 1]);
            b[i][j] = min(b[i][j - 1], b[i + (1 << j - 1)][j - 1]);
        }
    for (int i = 0; i < n; i++)
    {
        int lo = i, hi = n - 1, mi, L = -1, R = -1;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                L = mi;
                hi = mi - 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (L == -1) continue;
        lo = i, hi = n;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                R = mi;
                lo = mi + 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (R == -1) continue;
        ans += (R - L + 1);
    }
    //cout << getA(5, 6) << ' ' << getB(5, 6) << '\n';
    cout << ans;
}


#include<bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int n, m, a[N][25], b[N][25], Log[N], ans;
int getA(int l, int r) {
    int j = Log[r - l + 1];
    //cout << j << ' ' << a[l][j] << '\n';
    return max(a[l][j], a[r - (1 << j) + 1][j]);
}
int getB(int l, int r) {
    int j = Log[r - l + 1];
    //cout << j << ' ' << b[l][j] << '\n';
    return min(b[l][j], b[r - (1 << j) + 1][j]);
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i][0];
    for (int i = 0; i < n; i++) cin >> b[i][0];
    Log[0] = Log[1] = 0;
    for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;
    for (int j = 1; j < 18; j++)
        for (int i = 0; i + (1 << j) <= n; i++) {
            a[i][j] = max(a[i][j - 1], a[i + (1 << j - 1)][j - 1]);
            b[i][j] = min(b[i][j - 1], b[i + (1 << j - 1)][j - 1]);
        }
    for (int i = 0; i < n; i++)
    {
        int lo = i, hi = n + 1, mi, L = -1, R = -1;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                L = mi;
                hi = mi - 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (L == -1) continue;
        lo = i, hi = n;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                R = mi;
                lo = mi + 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (R == -1) continue;
        ans += (R - L + 1);
    }
    //cout << getA(5, 6) << ' ' << getB(5, 6) << '\n';
    cout << ans;
}


#include<bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int n, m, a[N][25], b[N][25], Log[N], ans;
int getA(int l, int r) {
    int j = Log[r - l + 1];
    return max(a[l][j], a[r - (1 << j)][j]);
}
int getB(int l, int r) {
    int j = Log[r - l + 1];
    return min(b[l][j], b[r - (1 << j)][j]);
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i][0];
    for (int i = 1; i <= n; i++) cin >> b[i][0];
    for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;
    for (int j = 1; j < 18; j++)
        for (int i = 1; i + (1 << j) <= n; i++) {
            a[i][j] = max(a[i][j - 1], a[i + (1 << j - 1)][j - 1]);
            b[i][j] = min(b[i][j - 1], b[i + (1 << j - 1)][j - 1]);
        }
    for (int i = 1; i <= n; i++)
    {
        int lo = 1, hi = n, mi, L = -1, R = -1;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                L = mi;
                hi = mi - 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        if (L == -1) continue;
        lo = 1, hi = n;
        while (lo <= hi) {
            mi = (lo + hi) / 2;
            if (getA(i, mi) == getB(i, mi)) {
                R = mi;
                lo = mi + 1;
            } else if (getA(i, mi) < getB(i, mi)) {
                lo = mi + 1;
            } else hi = mi - 1;
        }
        ans += (R - L + 1);
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[1<<20][20];
int Pb[1<<20][20];
int lg[1<<20];
// Pa = min , Pb = max
int ans = 0;
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> Pa[i][0] ;
	for(int i=1;i<=n;i++) cin >> Pb[i][0] ;
	for(int i=2;i<=n+1;i++) lg[i] = lg[i>>1] + 1;
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pa[i][j] = min(Pa[i][j-1],Pa[i+(1<<(j-1))][j-1]);
			Pb[i][j] = max(Pb[i][j-1],Pb[i+(1<<(j-1))][j-1]);
		}
	}
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int j=lg[n - i + 1];j>=0;j--){
        		int  p = 1 << j;
          		if (p + l <= n && Max(i,l+p) <  Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p))	r += p;
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[MAX+1][oo];
int Pb[MAX+1][oo];
int b[MAX+1], a[MAX+1];
int lg[MAX+5];
// Pa = min , Pb = max
int ans = 0;
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i], Pa[i][0] = a[i];
	for(int i=1;i<=n;i++) cin >> b[i], Pb[i][0] = b[i];
	for(int i=2;i<=oo;i++) lg[i] = lg[i>>1] + 1;
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)<=n;i++){
			Pa[i][j] = min(Pa[i][j-1],Pa[i+(1<<(j-1))][j-1]);
		}
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)<=n;i++){
			Pb[i][j] = max(Pb[i][j-1],Pb[i+(1<<(j-1))][j-1]);
		}
	}
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int p=(1<<lg[n - i + 1]);p>=1;p>>=1){
          		if (p + l <= n && Max(i,l+p) < Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p)){
        			r += p;//, cout << i << " " << p << " " << l << " " << r << endl;
        		}
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[MAX+1][oo];
int Pb[MAX+1][oo];
int b[MAX+1], a[MAX+1];
int lg[MAX+5];
// Pa = min , Pb = max
int ans = 0;
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i], Pa[i][0] = a[i];
	for(int i=1;i<=n;i++) cin >> b[i], Pb[i][0] = b[i];
	for(int i=2;i<=oo;i++) lg[i] = lg[i>>1] + 1;
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pa[i][j] = min(Pa[i][j-1],Pa[i+(1<<(j-1))][j-1]);
		}
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pb[i][j] = max(Pb[i][j-1],Pb[i+(1<<(j-1))][j-1]);
		}
	}
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int p=(1<<lg[n - i + 1]);p;p>>=1){
          		if (p + l <= n && Max(i,l+p) < Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p)){
        			r += p;//, cout << i << " " << p << " " << l << " " << r << endl;
        		}
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


# include <bits/stdc++.h>
# define scan(x) scanf("%d",&x)
using namespace std;
# define fi cin
# define fo cout
int dx[20][1 << 20];
int dy[20][1 << 20];
int lg[1 << 20];
int mx(int l,int r)
{
    int p = lg[r - l + 1];
    return max(dx[p][l],dx[p][r - (1 << p) + 1]);
}
int mn(int l,int r)
{
    int p = lg[r - l + 1];
    return min(dy[p][l],dy[p][r - (1 << p) + 1]);
}
int main(void)
{
    int n;
    scan(n);
    for (int i = 1;i <= n;++i) scan(dx[0][i]);
    for (int i = 1;i <= n;++i) scan(dy[0][i]);
    for (int p = 1;(1 << p) <= n;++p)
        for (int i = 1;i + (1 << p) - 1 <= n;++i)
            dx[p][i] = max(dx[p-1][i],dx[p-1][i + (1 << (p-1))]),
            dy[p][i] = min(dy[p-1][i],dy[p-1][i + (1 << (p-1))]);
    for (int i = 2;i <= n;++i)
        lg[i] = lg[i >> 1] + 1;
    long long ans = 0;
    for (int i = 1;i <= n;++i)
    if (mx(i,i) <= mn(i,i))
    {
        int l = i-1,r = i;
        for (int p = 1 << lg[n - i + 1];p;p >>= 1)
            {
                if (p + l <= n && mx(i,l+p) < mn(i,l+p)) l += p;
                if (p + r <= n && mx(i,r+p) <= mn(i,r+p)) r += p;
            }
        ans += r - l;
    }
    fo << ans << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[1<<20][20];
int Pb[1<<20][20];
int lg[1<<20];
// Pa = min , Pb = max
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> Pa[i][0] ;
	for(int i=1;i<=n;i++) cin >> Pb[i][0] ;
	for(int i=2;i<=n+1;i++) lg[i] = lg[i>>1] + 1;
	for(int p=1;(1<<p)<=n;p++){
		for(int i=1;i+(1<<p)-1<=n;i++){
			Pa[i][p] = max(Pa[i][p-1],Pa[i+(1<<(p-1))][p-1]);
			Pb[i][p] = min(Pb[i][p-1],Pb[i+(1<<(p-1))][p-1]);
		}
	}
	int ans = 0;
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int j=lg[n - i + 1];j>=0;j--){
        		int  p = 1 << j;
          		if (p + l <= n && Max(i,l+p) <  Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p))	r += p;
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[MAX+5][oo+5];
int Pb[MAX+5][oo+5];
int lg[MAX+5];
// Pa = min , Pb = max
int ans = 0;
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> Pa[i][0] ;
	for(int i=1;i<=n;i++) cin >> Pb[i][0] ;
	for(int i=2;i<=n+1;i++) lg[i] = lg[i>>1] + 1;
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pa[i][j] = min(Pa[i][j-1],Pa[i+(1<<(j-1))][j-1]);
			Pb[i][j] = max(Pb[i][j-1],Pb[i+(1<<(j-1))][j-1]);
		}
	}
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int p=(1<<lg[n - i + 1]);p;p>>=1){
          		if (p + l <= n && Max(i,l+p) < Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p)){
        			r += p;//, cout << i << " " << p << " " << l << " " << r << endl;
        		}
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[MAX+1][oo];
int Pb[MAX+1][oo];
int b[MAX+1], a[MAX+1];
int lg[MAX+5];
// Pa = min , Pb = max
int ans = 0;
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i], Pa[i][0] = a[i];
	for(int i=1;i<=n;i++) cin >> b[i], Pb[i][0] = b[i];
	for(int i=2;i<=oo;i++) lg[i] = lg[i>>1] + 1;
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pa[i][j] = min(Pa[i][j-1],Pa[i+(1<<(j-1))][j-1]);
		}
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pb[i][j] = max(Pb[i][j-1],Pb[i+(1<<(j-1))][j-1]);
		}
	}
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int p=(1<<lg[n - i + 1]);p>=1;p>>=1){
          		if (p + l <= n && Max(i,l+p) < Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p)){
        			r += p;//, cout << i << " " << p << " " << l << " " << r << endl;
        		}
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define int long long
const int oo = log2(MAX);
int n;
int Pa[MAX+1][oo];
int Pb[MAX+1][oo];
int b[MAX+1], a[MAX+1];
int lg[MAX+5];
// Pa = min , Pb = max
int ans = 0;
int Max(int l,int r){
	int p = lg[r-l+1];
	return max(Pb[l][p],Pb[r-(1<<p)+1][p]);
}
int Min(int l,int r){
	int p = lg[r-l+1];
	return min(Pa[l][p],Pa[r-(1<<p)+1][p]);
}
signed main(){
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i], Pa[i][0] = a[i];
	for(int i=1;i<=n;i++) cin >> b[i], Pb[i][0] = b[i];
	for(int i=2;i<=n;i++) lg[i] = lg[i>>1] + 1;
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pa[i][j] = min(Pa[i][j-1],Pa[i+(1<<(j-1))][j-1]);
		}
	}
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			Pb[i][j] = max(Pb[i][j-1],Pb[i+(1<<(j-1))][j-1]);
		}
	}
	for (int i=1;i<=n;i++){
   		if (Max(i,i) <= Min(i,i)){
        	int l = i-1,r = i;
        	for(int p=(1<<lg[n - i + 1]);p;p>>=1){
          		if (p + l <= n && Max(i,l+p) < Min(i,l+p)) l += p;
        		if (p + r <= n && Max(i,r+p) <= Min(i,r+p)){
        			r += p;//, cout << i << " " << p << " " << l << " " << r << endl;
        		}
        	}
        	ans += r - l;
    	}
    }
    cout << ans;
}


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

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

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

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

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


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

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

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

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

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


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

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

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

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

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


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

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

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

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

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


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

using namespace std;
typedef long long ll;

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

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

int main() {
    int N;
    cin >> N;
    ST<int> A(N);
    ST2<int> B(N);
    for (int i = 0; i < N; i++) {
        scanf("%d", a+i);
    }
    for (int i = 0; i < N; i++) {
        scanf("%d", b+i);
    }
    ll ans = 0;
    for (int i = N-1; i >= 0; i--) {
        A.update(i, a[i]);
        B.update(i, b[i]);
        if (a[i] > b[i]) continue;
        int l = i, r = i+1;
        int p = 1;
        while (p <= N-i)
            p *= 2;
        for (; p > 0; p/=2) {
            if (p+l <= N && A.query(i, l+p) < B.query(i, l+p)) l += p;
            if (p+r <= N && A.query(i, r+p) <= B.query(i, r+p)) r += p;
        }
        ans += r-l;
    }
    printf("%lld\n", ans);
    return 0;
}


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

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

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

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

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