#include <bits/stdc++.h>
using namespace std;
const string TASK = "Friends and Subsequences";
const int maxn = 2e5 + 1;
const int maxlg = 100;
int n, i, k, a[maxn], b[maxn], fa[maxn][maxlg], fb[maxn][maxlg], lg[maxn];
long long res;
void Enter() {
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> a[i], a[i] += 1e9;
  for (i = 1; i <= n; ++i) cin >> b[i], b[i] += 1e9;
}
void Init() {
  lg[1] = 0;
  for (i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
  for (i = 1; i <= n; ++i) fa[i][0] = a[i], fb[i][0] = b[i];
  for (k = 1; k <= lg[n]; ++k) {
    for (i = 1; i <= n; ++i) {
      int j = i + (1 << (k - 1));
      if (j > n) j = i;
      fa[i][k] = max(fa[i][k - 1], fa[j][k - 1]);
      fb[i][k] = min(fb[i][k - 1], fb[j][k - 1]);
    }
  }
}
int Search(bool cmp(int i, int j)) {
  int l = i;
  int r = n;
  while (l <= r) {
    int m = (l + r) / 2;
    int t = lg[m - i + 1];
    if (cmp(max(fa[i][t], fa[m - (1 << t) + 1][t]),
            min(fb[i][t], fb[m - (1 << t) + 1][t])))
      l = m + 1;
    else
      r = m - 1;
  }
  return l;
}
bool cmp_L(int a, int b) { return a < b; }
bool cmp_R(int a, int b) { return a <= b; }
void Solve() {
  res = 0;
  for (i = 1; i <= n; ++i) {
    int l = Search(cmp_L);
    int r = Search(cmp_R);
    res += r - l;
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Enter();
  Init();
  Solve();
}
