#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, a[N], b[N];
long long mn[N][22], mx[N][22];
signed main() {
  scanf("%lld", &n);
  for (long long i = (1); i <= (n); ++i) scanf("%lld", &a[i]), mx[i][0] = a[i];
  for (long long i = (1); i <= (n); ++i) scanf("%lld", &b[i]), mn[i][0] = b[i];
  for (long long j = (1); j <= (21); ++j) {
    for (long long i = (1); i <= (n); ++i) {
      if (i + (1 << (j - 1)) - 1 > n)
        mx[i][j] = mx[i][j - 1];
      else
        mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
    }
  }
  for (long long j = (1); j <= (21); ++j) {
    for (long long i = (1); i <= (n); ++i) {
      if (i + (1 << (j - 1)) - 1 > n)
        mn[i][j] = mn[i][j - 1];
      else
        mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
    }
  }
  long long ans = 0;
  for (long long i = (1); i <= (n); ++i) {
    long long l = i, r = n;
    long long rr = 0, ll = 0;
    while (r - l > 5) {
      long long mid = (l + r) >> 1;
      long long lg = log2(mid - i + 1);
      long long mxx = max(mx[i][lg], mx[mid - (1 << lg) + 1][lg]);
      long long mnn = min(mn[i][lg], mn[mid - (1 << lg) + 1][lg]);
      if (mxx > mnn)
        r = mid;
      else
        l = mid;
    }
    for (long long j = (r); j >= (l); --j) {
      long long lg = log2(j - i + 1);
      long long mxx = max(mx[i][lg], mx[j - (1 << lg) + 1][lg]);
      long long mnn = min(mn[i][lg], mn[j - (1 << lg) + 1][lg]);
      if (mxx == mnn) {
        rr = j;
        break;
      }
    }
    l = i, r = n;
    while (r - l > 5) {
      long long mid = (l + r) >> 1;
      long long lg = log2(mid - i + 1);
      long long mxx = max(mx[i][lg], mx[mid - (1 << lg) + 1][lg]);
      long long mnn = min(mn[i][lg], mn[mid - (1 << lg) + 1][lg]);
      if (mxx >= mnn)
        r = mid;
      else
        l = mid;
    }
    for (long long j = (l); j <= (r); ++j) {
      long long lg = log2(j - i + 1);
      long long mxx = max(mx[i][lg], mx[j - (1 << lg) + 1][lg]);
      long long mnn = min(mn[i][lg], mn[j - (1 << lg) + 1][lg]);
      if (mxx == mnn) {
        ll = j;
        break;
      }
    }
    if (ll && rr) ans += (rr - ll + 1);
  }
  printf("%lld", ans);
  return 0;
}
