#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
long long a[N], b[N];
deque<int> mx, mn;
int main() {
  long long n;
  cin >> n;
  long long i;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) cin >> b[i];
  long long j, ans = 0;
  for (j = 1, i = 1; i <= n; i++) {
    while (!mx.empty() and a[mx.back()] <= a[i]) mx.pop_back();
    while (!mn.empty() and b[mn.back()] >= b[i]) mn.pop_back();
    mx.push_back(i), mn.push_back(i);
    while (j <= i and a[mx.front()] - b[mn.front()] > 0) {
      j++;
      while (!mx.empty() and mx.front() < j) mx.pop_front();
      while (!mn.empty() and mn.front() < j) mn.pop_front();
    }
    if (!mx.empty() and !mn.empty() and a[mx.front()] == b[mn.front()])
      ans += min(mx.front(), mn.front()) - j + 1;
  }
  cout << ans << endl;
  return 0;
}
