#include <bits/stdc++.h>
using namespace std;
struct __io_dev {
  __io_dev(const bool& __fastio = false) {
    if (__fastio) ios_base::sync_with_stdio(false), cin.tie(nullptr);
    srand(time(nullptr));
    if (!string("").empty())
      freopen(
          ""
          ".in",
          "r", stdin),
          freopen(
              ""
              ".out",
              "w", stdout);
  }
  ~__io_dev() { fprintf(stderr, "%.6f ms\n", 1e3 * clock() / CLOCKS_PER_SEC); }
} __io(false);
const long long inf = (long long)1e+9 + 7ll;
const long long linf = (long long)1e+18 + 7ll;
const long double eps = (long double)1e-9;
const long double pi = acosl((long double)-1.0);
const int alph = 26;
const int sqr = 3017;
static char buff[(int)2e6 + 17];
long long __p[3] = {29ll, 31ll, 33ll};
long long __mod[3] = {inf, inf + 2ll, 14881337ll};
const int maxn = (int)2e5 + 17;
int n;
int a[maxn], b[maxn], lg[maxn];
long long sta[21][maxn], stb[21][maxn];
void build() {
  for (int i = 1, l = 0; i < maxn; ++i) l += (i == (1 << (l + 1))), lg[i] = l;
  for (long long i = 0ll; i < (long long)n; ++i)
    sta[0][i] = a[i], stb[0][i] = b[i];
  for (int k = 1; k <= lg[n]; ++k)
    for (int i = 0; i + (1 << (k - 1)) < n; ++i) {
      sta[k][i] = max(sta[k - 1][i], sta[k - 1][i + (1 << (k - 1))]);
      stb[k][i] = min(stb[k - 1][i], stb[k - 1][i + (1 << (k - 1))]);
    }
}
long long getdiff(int l, int r) {
  int k = lg[r - l + 1];
  return max(sta[k][l], sta[k][r - (1 << k) + 1]) -
         min(stb[k][l], stb[k][r - (1 << k) + 1]);
}
int main() {
  scanf("%d", &n);
  for (long long i = 0ll; i < (long long)n; ++i) scanf("%d", a + i);
  for (long long i = 0ll; i < (long long)n; ++i) scanf("%d", b + i);
  build();
  long long ans = 0;
  for (long long i = 0ll; i < (long long)n; ++i) {
    if (a[i] > b[i]) continue;
    int lf, rg, l, r;
    if (a[i] < b[i]) {
      for (l = i, r = n - 1; l < r;) {
        int m = (l + r + 1) / 2;
        if (getdiff(i, m) < 0)
          l = m;
        else
          r = m - 1;
      }
      lf = l;
    } else
      lf = i - 1;
    if (getdiff(i, n - 1) > 0) {
      for (l = i, r = n - 1; l < r;) {
        int m = (l + r) / 2;
        if (getdiff(i, m) > 0)
          r = m;
        else
          l = m + 1;
      }
      rg = l;
    } else
      rg = n;
    ans += rg - lf - 1;
  }
  printf("%lld\n", ans);
  return 0;
}
