#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int N = 2e5 + 5;
const int LN = 30;
const int MAX = 2e6 + 5;
const int MOD = 1000000007;
int n;
int a[N], b[N];
int dp_min[N][LN];
int dp_max[N][LN];
void build_min() {
  for (int i = 1; i <= n; i++) dp_min[i][0] = b[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      dp_min[i][j] = min(dp_min[i][j - 1], dp_min[i + (1 << (j - 1))][j - 1]);
    }
  }
}
void build_max() {
  for (int i = 1; i <= n; i++) dp_max[i][0] = a[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      dp_max[i][j] = max(dp_max[i][j - 1], dp_max[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int query_min(int l, int r) {
  int k = log2(r - l + 1);
  return min(dp_min[l][k], dp_min[r - (1 << k) + 1][k]);
}
int query_max(int l, int r) {
  int k = log2(r - l + 1);
  return max(dp_max[l][k], dp_max[r - (1 << k) + 1][k]);
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  srand(time(NULL));
  cout << fixed << setprecision(10);
  clock_t clk;
  clk = clock();
  ;
  cin >> n;
  for (long long i = (1); i <= (n); ++i) cin >> a[i];
  for (long long i = (1); i <= (n); ++i) cin >> b[i];
  build_max();
  build_min();
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > b[i]) continue;
    int l, r, mid, r1, r2;
    l = i, r = n;
    while (l < r) {
      mid = (l + r) >> 1;
      if (query_max(i, mid) >= query_min(i, mid)) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (query_max(i, l) != query_min(i, l)) continue;
    r1 = l;
    l = i, r = n;
    while (l < r) {
      mid = (l + r + 1) >> 1;
      if (query_max(i, mid) <= query_min(i, mid)) {
        l = mid;
      } else {
        r = mid - 1;
      }
    }
    if (query_max(i, l) != query_min(i, l)) continue;
    r2 = l;
    ans += (r2 - r1 + 1);
  }
  cout << ans << "\n";
  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
  ;
}
