#include <bits/stdc++.h>
using namespace std;
template <typename T>
T read1() {
  T t = 0;
  char k;
  bool v = 0;
  do (k = getchar()) == '-' && (v = 1);
  while ('0' > k || k > '9');
  while ('0' <= k && k <= '9')
    t = (t << 3) + (t << 1) + (k ^ '0'), k = getchar();
  return v ? -t : t;
}
int a[20][200005], b[20][200005], s, w[200005];
int judge(int l, int r, int &x1) {
  int n = w[r - l + 1], x = r - (1 << n) + 1;
  int f = max(a[n][l], a[n][x]), g = min(b[n][l], b[n][x]);
  return f < g ? -1 : (f == g ? (x1 = r, 0) : 1);
}
int main() {
  s = read1<long long>();
  for (int i = 2; i <= s; ++i) w[i] = w[i >> 1] + 1;
  for (int i = 1; i <= s; ++i) a[0][i] = read1<long long>();
  for (int i = 1; i <= s; ++i) b[0][i] = read1<long long>();
  for (int i = 1; (1 << i) <= s; ++i)
    for (int j = 1; j + (1 << i) - 1 <= s; ++j)
      a[i][j] = max(a[i - 1][j], a[i - 1][j + (1 << i - 1)]),
      b[i][j] = min(b[i - 1][j], b[i - 1][j + (1 << i - 1)]);
  long long ans = 0;
  for (int i = 1; i <= s; ++i) {
    int l = i, r = s, mid, o = 0, p = 0;
    while (l <= r)
      judge(i, mid = l + r >> 1, o) <= 0 ? l = mid + 1 : r = mid - 1;
    l = i, r = s;
    while (l <= r)
      judge(i, mid = l + r >> 1, p) < 0 ? l = mid + 1 : r = mid - 1;
    if (o) ans += o - p + 1;
  }
  cout << ans;
  return 0;
}
