#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	int slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout <<  d.x <<" " << d.y << " "<< c.x << " " << c.y  << endl;

	}
	else
	{

			slope=((b.y -a.y) / (b.x - a.x) );
			if(( slope != 0 ) && ( slope != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}

	    	cout <<  d.x <<" " << d.y << " "<< c.x << " " << c.y  << endl;
    	}
	}
return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    struct point{
        int x, y;
    };
    point a, b, c, d;
    int delta;
    cin >> a.x >> a.y >> b.x >> b.y;
            if ( a.y == b.y )
            {
                delta = abs( a.x - b.x);
                c.x = a.x;
                c.y = a.y + delta;
                d.x = b.x;
                d.y = b.y + delta;
				if (abs(c.y - b.y) != abs(d.x - c.x))
					cout<<"-1"<<endl;
				else
					cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
            }
            else
                if ( a.x == b.x)
                {
                    delta = abs(a.y - b.y);
                    c.y = a.y;
                    c.x = a.x + delta;
                    d.y = b.y;
                    d.x = b.x + delta;
					if (abs(c.x - a.x) != abs(a.y - b.y))
					cout<<"-1"<<endl;
				else
					cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
                }
				else
                     {
						 c.x = a.x;
						 c.y = b.y ;
						 d.x = b.x ;
						 d.y = a.y;

						 if ( abs(a.x - d.x) != abs(c.y - a.y))
							 cout<< "-1"<<endl;
						 else
							 	cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
                     }
   return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	float slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

	}
	else
	{

			slope=((b.y -a.y) * 1.0 / (b.x - a.x) );
			if(( slope != 0 ) && ( abs(slope) != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}
				if ( slope == -1)
				    cout << d.x << " " << -d.y << " " << c.x << " " << -c.y << endl;
				else
				    cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

			}
	}
return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	float slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

	}
	else
	{

			slope=((b.y -a.y) * 1.0 / (b.x - a.x) );
			if(( slope != 0 ) && ( slope != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}
				cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

			}
	}
return 0;
}


#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	int slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout << "c = (" << c.x << ',' << c.y << ')' << endl;
		cout << "d = (" << d.x << ',' << d.y << ')' << endl;

	}
	else
	{

			slope=((b.y -a.y) / (b.x - a.x) );
			if(( slope != 0 ) && ( slope != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}

				cout << "c = (" << c.x << ',' << c.y << ')' << endl;
				cout << "d = (" << d.x << ',' << d.y << ')' << endl;
			}
	}

return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    struct point{
        int x, y;
    };
    point a, b, c, d;
    int delta;
    cin >> a.x >> a.y >> b.x >> b.y;
            if ( a.y == b.y )
            {
                delta = abs( a.x - b.x);
                c.x = a.x;
                c.y = a.y + delta;
                d.x = b.x;
                d.y = b.y + delta;
				if (abs(c.y - b.y) != abs(d.x - c.x))
					cout<<"-1"<<endl;
				else
					cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
            }
            else
                if ( a.x == b.x)
                {
                    delta = abs(a.y - b.y);
                    c.y = a.y;
                    c.x = a.x + delta;
                    d.y = b.y;
                    d.x = b.x + delta;
					if (abs(c.x - c.x) != abs(c.y - b.y))
					cout<<"-1"<<endl;
				else
					cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
                }
				else
                     {
						 c.x = a.x;
						 c.y = b.y ;
						 d.x = b.x ;
						 d.y = a.y;

						 if ( abs(a.x - d.x) != abs(c.y - a.y))
							 cout<< "-1"<<endl;
						 else
							 	cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
                     }
   return 0;
}


#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	int slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout <<  d.x <<" " << d.y << " "<< c.x << " " << d.y  << endl;

	}
	else
	{

			slope=((b.y -a.y) / (b.x - a.x) );
			if(( slope != 0 ) && ( slope != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}

					cout <<  d.x <<" " << d.y << " "<< c.x << " " << d.y  << endl;
			}
	}
return 0;
}


#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	int slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout <<  c.x <<  c.y << d.x <<  d.y  << endl;

	}
	else
	{

			slope=((b.y -a.y) / (b.x - a.x) );
			if(( slope != 0 ) && ( slope != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}

					cout <<  c.x <<  c.y << d.x <<  d.y  << endl;
			}
	}
return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	float slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

	}
	else
	{

			slope=((b.y -a.y) * 1.0 / (b.x - a.x) );
			if(( slope != 0 ) && ( abs(slope) != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}
				cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

			}
	}
return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    struct point{
        int x, y;
    };
    point a, b, c, d;
    int delta;
    cin >> a.x >> a.y >> b.x >> b.y;
     if (abs (a.x - b.x) != abs(a.y - b.y) )
        cout<< "-1";
        else
        {
            if ( a.y == b.y )
            {
                delta = abs( a.x - b.x);
                c.x = a.x;
                c.y = a.y + delta;
                d.x = b.x;
                d.y = b.y + delta;
            }
            else
                if ( a.x == b.x)
                {
                    delta = abs(a.y - b.y);
                    c.y = a.y;
                    c.x = a.x + delta;
                    d.y = b.y;
                    d.x = b.x + delta;
                }
                else
                 {
                     delta = abs ( a.x - b.x);
                     if ( (a.x > a.y)&& (b.y > a.y))
                     {
                     c.x = a.x;
                     c.y = a.y + delta;
                     d.x = a.x + delta;
                     d.y = a.y;
                     }
                     else
                     {
                         c.x = a.x;
                         c.y = a.y - delta;
                         d.x = a.x + delta;
                         d.y = b.y;
                     }
                 }
            cout << c.x << " " << c.y << " " << d.x << " " << d.y << endl;
        }

}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	float slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

	}
	else
	{

			slope=((b.y -a.y) * 1.0 / (b.x - a.x) );
			if(( slope != 0 ) && ( abs(slope) != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}
				if ( slope == -1)
				    cout << -d.x << " " << -d.y << " " << -c.x << " " << -c.y << endl;
				else
				    cout << d.x << " " << d.y << " " << c.x << " " << c.y << endl;

			}
	}
return 0;
}


#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
	struct point{
		int x, y;
	};
	point a, b, c, d;
	int max_x, max_y, min_x, min_y, d_x, d_y;
	cin >> a.x >> a.y >> b.x >> b.y;
	int slope;

	if ( (b.x - a.x ) * 1.0 / ( b.y - a.y ) == 0)
	{
		if ( a.y > b.y )
		{ max_y = a.y; min_y = b.y; }
		else
		{ max_y = b.y; min_y = a.y; }
		d_y = max_y - min_y;
		c.x = a.x + d_y;
		d.x = c.x;
		c.y = max_y;
		d.y = min_y;

		cout <<  c.x <<  c.y << d.x <<  d.y  << endl;

	}
	else
	{

			slope=((b.y -a.y) / (b.x - a.x) );
			if(( slope != 0 ) && ( slope != 1 ))
				cout << "-1\n";
			else
			{

				if ( slope == 0)
				{
					if ( a.x > b.x)
					{ max_x = a.x; min_x = b.x;

					}
					else
					{ max_x  = b.x; min_x = a.x;}

					d_x = max_x - min_x;
					c.x = min_x;
					c.y = a.y + d_x;
					d.x = max_x;
					d.y = c.y ;

				}
				if ( abs(slope) == 1)
				{
					if (a.x > b.x)
					{ max_x = a.x; min_x = b.x; }
					else
					{ max_x = b.x; min_x = a.x; }
					if ( a.y > b.y )
					{ max_y = a.y; min_y = b.y; }
					else
					{ max_y = b.y; min_y = a.y; }
					d_x = max_x - min_x;
					c.x = min_x;
					c.y = min_y + d_x;
					d.x = max_x;
					d.y = min_y;

				}

					cout <<  c.x <<  c.y << d.x <<  d.y  << endl;
			}
	}

system("pause");
return 0;
}


#include<iostream>
using namespace std;
//Pashmak and Garden

int main() {
	int x1, y1, x2, y2, x3, y3, x4, y4, y_max = y1, x_max = x1, y_min = y2, x_min=x2;
	cin >> x1 >> y1 >> x2 >> y2;

	if (y2 > y_max)
	{
		y_max = y2;
		y_min = y1;
	}
	if (x2 > x_max) {
		x_max = x2;
		x_min = x1;
	}

	int y_length = y_max - y_min;
	int x_length = x_max - x_min;

		//y axis
		if (x1 == x2) {
			y3 = y1;
			y4 = y2;
			if (x1 == x_max)
			{
				x3 = x_min;
				x4 = x_min;
			}
			else
			{
				x3 = x_max;
				x4 = x_max;
			}
		}
		//x axis
		else if (y1 == y2) {
			x3 = x1;
			x4 = x2;
			if (y1 == y_max)
			{
				y3 = y_min;
				y4 = y_min;
			}
			else
			{
				y3 = y_max;
				y4 = y_max;
			}
		}
		//Diagonal
		else {
			x3 = x1;
			x4 = x2;
			if (y1 == y_max)
			{
				y3 = y_min;
				y4 = y_max;
			}
			else
			{
				y3 = y_max;
				y4 = y_min;
			}
		}

			cout << x3 << " " << y3 << " " << x4 << " " << y4 << endl;

	return 0;
}


#include<iostream>
using namespace std;
//Pashmak and Garden

int main() {
	int x1, y1, x2, y2, x3, y3, x4, y4, y_max = y1, x_max = x1, y_min = y2, x_min=x2;
	cin >> x1 >> y1 >> x2 >> y2;

	if (y2 > y_max)
	{
		y_max = y2;
		y_min = y1;
	}
	if (x2 > x_max) {
		x_max = x2;
		x_min = x1;
	}

	int y_length = y_max - y_min;
	int x_length = x_max - x_min;

	if (y_length != x_length)
		cout << "-1" << endl;
	else {
		//y axis
		if (x1 == x2) {
			y3 = y1;
			y4 = y2;
			if (x1 == x_max)
			{
				x3 = x_min;
				x4 = x_min;
			}
			else
			{
				x3 = x_max;
				x4 = x_max;
			}
		}
		//x axis
		else if (y1 == y2) {
			x3 = x1;
			x4 = x2;
			if (y1 == y_max)
			{
				y3 = y_min;
				y4 = y_min;
			}
			else
			{
				y3 = y_max;
				y4 = y_max;
			}
		}
		//Diagonal
		else {
			x3 = x1;
			x4 = x2;
			if (y1 == y_max)
			{
				y3 = y_min;
				y4 = y_max;
			}
			else
			{
				y3 = y_max;
				y4 = y_min;
			}
		}
		cout << x3 << " " << y3 << " " << x4 << " " << y4 << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef int ll ;
int mini[800000],maxi[800000];
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)>>1,l,r),qmax(node*2+1,((s+e)>>1)+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)>>1,l,r),qmin(node*2+1,((s+e)>>1)+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;

    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    long long ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(  qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if( qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if(  (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if( qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>
using namespace std;
typedef int ll ;
int mini[800000],maxi[800000];
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)>>1);
    build(node*2+1,((s+e)>>1)+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(l>e || r<s){
            return -1e9;
        }
        if(l<=s && e<=r){
            return maxi[node];
        }
        int mid=(s+e)>>1;
        int Q1,Q2;
        Q1=qmax(node*2,s,mid,l,r);
        Q2=qmax(node*2+1,mid+1,e,l,r);
        return max(Q1,Q2);
}
int qmin(int node , int s , int e , int l , int r)
{
    if(l>e || r<s){
            return 1e9;
        }
        if(l<=s && e<=r){
            return mini[node];
        }
        int mid=(s+e)>>1;
        int Q1,Q2;
        Q1=qmin(node*2,s,mid,l,r);
        Q2=qmin(node*2+1,mid+1,e,l,r);
        return min(Q1,Q2);
}
int main()
{
    //ios::sync_with_stdio(0);
   // cin.tie(0);
    int n ;
    cin >> n ;

    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    long long ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+((e-s)>>1);

            if(  qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) == 0) e = mid-1 , S = mid;
            else if( qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if(qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+((e-s)>>1);
            if(  (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) > 0) e = mid-1;
            else if( qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


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

// defines
#define KhaledAshraf ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define modulo 1000000007
#define pi 3.1415926535897932384

typedef long long int lln;

//to be able to use to_string  patch::to_string("char or string")

namespace patch
{
template < typename T > std::string to_string( const T& n )
{
    std::ostringstream stm ;
    stm << n ;
    return stm.str() ;
}
}

using namespace std;
lln N,arr[200005],brr[200005];
lln mnTree[500000],mxTree[500000];

struct SegTree{
    void Build(int Node,int S,int E){
        if(S==E){
            mnTree[Node]=brr[S];
            mxTree[Node]=arr[S];
            return;
        }
        lln mid=(S+E)>>1;
        Build(Node*2,S,mid);
        Build(Node*2+1,mid+1,E);
        mnTree[Node]=min(mnTree[Node*2],mnTree[Node*2+1]);
        mxTree[Node]=max(mxTree[Node*2],mxTree[Node*2+1]);
    }
    lln maxQuery(int Node,int S,int E,int L,int R){
        if(L>E || R<S){
            return -1e18;
        }
        if(L<=S && E<=R){
            return mxTree[Node];
        }
        lln mid=(S+E)>>1;
        lln Q1,Q2;
        Q1=maxQuery(Node*2,S,mid,L,R);
        Q2=maxQuery(Node*2+1,mid+1,E,L,R);
        return max(Q1,Q2);
    }
    lln minQuery(int Node,int S,int E,int L,int R){
        if(L>E || R<S){
            return 1e18;
        }
        if(L<=S && E<=R){
            return mnTree[Node];
        }
        lln mid=(S+E)>>1;
        lln Q1,Q2;
        Q1=minQuery(Node*2,S,mid,L,R);
        Q2=minQuery(Node*2+1,mid+1,E,L,R);
        return min(Q1,Q2);
    }

};

int main()
{
    KhaledAshraf
    lln N;
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>arr[i];
    }
    for(int i=0;i<N;i++){
        cin>>brr[i];
    }
    SegTree ST;
    ST.Build(1,0,N-1);
    lln ans=0,S,E,M,res1=-1,res2=-1;
    for(int i=0;i<N;i++){
        S=i,E=N-1;
        res1=-1,res2=-1;
        lln mnval,mxval;
        while(S<=E){
            M=S+(E-S)/2;
            mnval=ST.minQuery(1,0,N-1,i,M);
            mxval=ST.maxQuery(1,0,N-1,i,M);
            if(mxval>mnval){
                E=M-1;
            }
            else{
                if(mxval==mnval){
                    res1=M;
                }
                S=M+1;
            }
        }
        S=i,E=N-1;
        while(S<=E){
            M=S+(E-S)/2;
            mnval=ST.minQuery(1,0,N-1,i,M);
            mxval=ST.maxQuery(1,0,N-1,i,M);
            if(mxval>=mnval){
                if(mxval==mnval){
                    res2=M;
                }
                E=M-1;
            }
            else{
                S=M+1;
            }
        }

        if(res1!=-1 && res2!=-1){
            //cout<<res1<<" "<<res2<<endl;
            ans+=res1-res2+1;
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef int ll ;
int mini[800000],maxi[800000];
int arr[(int) 2e5+5] , brr[(int) 2e5+5];
void build(int node, int s, int e)
{
    if(s == e)
    {
        mini[node] = brr[s];
        maxi[node] = arr[s];
        return ;
    }
    build(node*2,s,(s+e)/2);
    build(node*2+1,(s+e)/2+1,e);
    mini[node] = min(mini[node*2],mini[node*2+1]);
    maxi[node] = max(maxi[node*2],maxi[node*2+1]);
}
int qmax(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return -1e9 ;
    if(l <= s && e <= r) return maxi[node] ;
    return max(qmax(node*2,s,(s+e)>>1,l,r),qmax(node*2+1,((s+e)>>1)+1,e,l,r));
}
int qmin(int node , int s , int e , int l , int r)
{
    if(s > r || e < l) return 1e9 ;
    if(l <= s && e <= r) return mini[node] ;
    return min(qmin(node*2,s,(s+e)>>1,l,r),qmin(node*2+1,((s+e)>>1)+1,e,l,r));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;

    for(int i = 0 ; i < n ; i++) cin >> arr[i];
    for(int i = 0 ; i < n ; i++) cin >> brr[i];
    build(1,0,n-1);
    int ans = 0;
    for(int i= 0 ; i < n ; i++)
    {
        int s = i , e = n-1 , S = -1 , E = -1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) e = mid-1 , S = mid;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if((ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        s = i , e = n-1 ;
        while( s <= e)
        {
            int mid = s+(e-s)/2;
            if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) == 0) s = mid+1 , E = mid;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) > 0) e = mid-1;
            else if( (ll) (qmax(1,0,n-1,i,mid)- qmin(1,0,n-1,i,mid)) < 0) s = mid+1;
        }
        if(S != -1 && E != -1) ans+= E-S+1;
    }
    cout << ans << "\n" ;
}


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

int main( void ) {
	ll n;
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
		ll idx;
		if( mb.find( b[i] ) == mb.end() ) {
			idx = mb.size();
			mb[b[i]] = idx;
		} else {
			idx = mb[b[i]];
		}
		v[idx].pb( i );
	}
	la[0] = 0;
	lb[0] = 0;
	for( ll i = 1; i < n; i ++ ) {
		if( a[i] >= a[i-1] )
			la[i] = la[i-1];
		else
			la[i] = i;
		if( b[i] <= b[i-1] )
			lb[i] = lb[i-1];
		else
			lb[i] = i;
	}
	ra[n-1] = n-1;
	rb[n-1] = n-1;
	for( ll i = n-2; i >= 0; i -- ) {
		if( a[i] >= a[i+1] )
			ra[i] = ra[i+1];
		else
			ra[i] = i;
		if( b[i] <= b[i+1] )
			rb[i] = rb[i+1];
		else
			rb[i] = i;
	}
	for( ll i = 0; i < n; i ++ ) {
		if( la[i]-1 >= 0 && a[i] >= a[la[i]-1] )
			la[i] = la[la[i]-1];
		if( lb[i]-1 >= 0 && b[i] <= b[lb[i]-1] )
			lb[i] = lb[lb[i]-1];
	}
	for( ll i = n-1; i >= 0; i -- ) {
		if( ra[i]+1 < n && a[i] >= a[ra[i]+1] )
			ra[i] = ra[ra[i]+1];
		if( rb[i]+1 < n && b[i] <= b[rb[i]+1] )
			rb[i] = rb[rb[i]+1];
	}
	forn( i, n ) {
		pair< ll, ll > p = findAinB( i );
		ll L = p.X;
		ll R = p.Y;
		if( L == -1 && R == -1 )
			continue;
		if( add( i, L ) )
			continue;
		if( add( i, R ) )
			continue;
	}
	cout << ans << endl;
	return 0;
}

/*

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

ll A[33][211111];
ll B[33][211111];

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

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

ll get( ll l, ll r, ll i ) {
	ll a = i - l + 1;
	ll b = r - i + 1;
	return a * b;
}

int main( void ) {
	ll n;
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
	}
	la[0] = 0;
	lb[0] = 0;
	for( ll i = 1; i < n; i ++ ) {
		if( a[i] >= a[i-1] )
			la[i] = la[i-1];
		else
			la[i] = i;
		if( b[i] <= b[i-1] )
			lb[i] = lb[i-1];
		else
			lb[i] = i;
	}
	ra[n-1] = n-1;
	rb[n-1] = n-1;
	for( ll i = n-2; i >= 0; i -- ) {
		if( a[i] >= a[i+1] )
			ra[i] = ra[i+1];
		else
			ra[i] = i;
		if( b[i] <= b[i+1] )
			rb[i] = rb[i+1];
		else
			rb[i] = i;
	}
	for( ll i = 0; i < n; i ++ ) {
		if( la[i]-1 >= 0 && a[i] >= a[la[i]-1] )
			la[i] = la[la[i]-1];
		if( lb[i]-1 >= 0 && b[i] <= b[lb[i]-1] )
			lb[i] = lb[lb[i]-1];
	}
	for( ll i = n-1; i >= 0; i -- ) {
		if( ra[i]+1 < n && a[i] >= a[ra[i]+1] )
			ra[i] = ra[ra[i]+1];
		if( rb[i]+1 < n && b[i] <= b[rb[i]+1] )
			rb[i] = rb[rb[i]+1];
	}
	ll ans = 0;
	map< pair< ll, ll >, ll > m;
	forn( i, n ) {
		if( a[i] != b[i] )
			continue;
		ll l = max( la[i], lb[i] );
		ll r = min( ra[i], rb[i] );
		ll L = l;
		if( m.find( mp( l, r ) ) != m.end() ) {
			l = m[mp( l, r )] + 1;
		}
		ans += get( l, r, i );
		m[mp( L, r )] = i;
	}
	cout << ans << endl;
	return 0;
}

/*

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

/*

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

int main( void ) {
	ll n;
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
		ll idx;
		if( mb.find( b[i] ) == mb.end() ) {
			idx = mb.size();
			mb[b[i]] = idx;
		} else {
			idx = mb[b[i]];
		}
		v[idx].pb( i );
	}
	la[0] = 0;
	lb[0] = 0;
	for( ll i = 1; i < n; i ++ ) {
		if( a[i] >= a[i-1] )
			la[i] = la[i-1];
		else
			la[i] = i;
		if( b[i] <= b[i-1] )
			lb[i] = lb[i-1];
		else
			lb[i] = i;
	}
	ra[n-1] = n-1;
	rb[n-1] = n-1;
	for( ll i = n-2; i >= 0; i -- ) {
		if( a[i] >= a[i+1] )
			ra[i] = ra[i+1];
		else
			ra[i] = i;
		if( b[i] <= b[i+1] )
			rb[i] = rb[i+1];
		else
			rb[i] = i;
	}
	for( ll i = 0; i < n; i ++ ) {
		if( la[i]-1 >= 0 && a[i] >= a[la[i]-1] )
			la[i] = la[la[i]-1];
		if( lb[i]-1 >= 0 && b[i] <= b[lb[i]-1] )
			lb[i] = lb[lb[i]-1];
	}
	for( ll i = n-1; i >= 0; i -- ) {
		if( ra[i]+1 < n && a[i] >= a[ra[i]+1] )
			ra[i] = ra[ra[i]+1];
		if( rb[i]+1 < n && b[i] <= b[rb[i]+1] )
			rb[i] = rb[rb[i]+1];
	}
	forn( i, n ) {
		pair< ll, ll > p = findAinB( i );
		ll L = p.X;
		ll R = p.Y;
		if( L == -1 && R == -1 )
			continue;
		if( L == -1 || R == -1 ) {
			if( add( i, L ) || add( i, R ) )
				ma[a[i]] = i + 1;
			continue;
		}
		if( L == R ) {
			if( add( i, L ) )
				ma[a[i]] = i + 1;
			continue;
		}
		if( add( i, L ) ) {
			ma[a[i]] = i;
			add( i, R );
			ma[a[i]] = i + 1;
			continue;
		}
		if( add( i, R ) ) {
			ma[a[i]] = i + 1;
		}
		/*
		if( add( i, L ) )
			ma[a[i]] = i;
		if( L != R )
			add( i, R );
		ma[a[i]] = i + 1;*/
	}
	cout << ans << endl;
	return 0;
}

/*

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

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

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

int main( void ) {
	ll n;
	cin >> n;
	forn( i, n ) {
		cin >> a[i];
	}
	forn( i, n ) {
		cin >> b[i];
	}
	la[0] = 0;
	lb[0] = 0;
	for( ll i = 1; i < n; i ++ ) {
		if( a[i] >= a[i-1] )
			la[i] = la[i-1];
		else
			la[i] = i;
		if( b[i] <= b[i-1] )
			lb[i] = lb[i-1];
		else
			lb[i] = i;
	}
	ra[n-1] = n-1;
	rb[n-1] = n-1;
	for( ll i = n-2; i >= 0; i -- ) {
		if( a[i] >= a[i+1] )
			ra[i] = ra[i+1];
		else
			ra[i] = i;
		if( b[i] <= b[i+1] )
			rb[i] = rb[i+1];
		else
			rb[i] = i;
	}
	for( ll i = 0; i < n; i ++ ) {
		if( la[i]-1 >= 0 && a[i] >= a[la[i]-1] )
			la[i] = la[la[i]-1];
		if( lb[i]-1 >= 0 && b[i] <= b[lb[i]-1] )
			lb[i] = lb[lb[i]-1];
	}
	for( ll i = n-1; i >= 0; i -- ) {
		if( ra[i]+1 < n && a[i] >= a[ra[i]+1] )
			ra[i] = ra[ra[i]+1];
		if( rb[i]+1 < n && b[i] <= b[rb[i]+1] )
			rb[i] = rb[rb[i]+1];
	}
	ll ans = 0;
	//map< pair< ll, ll >, ll > m;
	ll L = -1;
	forn( i, n ) {
		if( a[i] != b[i] )
			continue;
		ll l = max( la[i], lb[i] );
		l = max( l, L );
		ll r = min( ra[i], rb[i] );
		//ll L = l;
		//if( m.find( mp( l, r ) ) != m.end() ) {
		//	l = m[mp( l, r )] + 1;
		//}
		ans += get( l, r, i );
		//m[mp( L, r )] = i;
		L = i + 1;
	}
	cout << ans << endl;
	return 0;
}

/*

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

ll pow2[211111];

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

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

vector< ll > v[211111];

map< ll, ll > ma, mb;

ll ans;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*

5
4 0 2 2 0
1 0 1 1 4

5
2 2 0 1 2
2 4 3 2 2

5
2 3 2 2 2
3 4 3 4 3

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