#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
const long long K = 20;
long long a[N], b[N];
pair<long long, long long> seg[N];
long long Log[N], stMin[N][K + 1], stMax[N][K + 1];
void filler(long long n) {
  Log[1] = 0;
  for (long long i = 2; i < N; i++) Log[i] = Log[i / 2] + 1;
  for (long long i = 0; i < n; i++) {
    stMin[i][0] = b[i];
    stMax[i][0] = a[i];
  }
  for (long long j = 1; j <= K; j++) {
    for (long long i = 0; i + (1 << j) <= n; i++) {
      stMin[i][j] = min(stMin[i][j - 1], stMin[i + (1 << (j - 1))][j - 1]);
      stMax[i][j] = max(stMax[i][j - 1], stMax[i + (1 << (j - 1))][j - 1]);
    }
  }
}
long long queryMin(long long l, long long r) {
  long long j = Log[r - l + 1];
  return min(stMin[l][j], stMin[r - (1 << j) + 1][j]);
}
long long queryMax(long long l, long long r) {
  long long j = Log[r - l + 1];
  return max(stMax[l][j], stMax[r - (1 << j) + 1][j]);
}
long long bs1(long long i, long long n) {
  long long l = i, r = n - 1;
  long long ans = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    pair<long long, long long> p = {queryMax(i, mid), queryMin(i, mid)};
    long long diff = p.first - p.second;
    if (diff == 0) {
      ans = mid;
    }
    if (diff < 0) {
      l = mid + 1;
    } else if (diff >= 0) {
      r = mid - 1;
    }
  }
  return ans;
}
long long bs2(long long i, long long n) {
  long long l = i, r = n - 1;
  long long ans = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    pair<long long, long long> p = {queryMax(i, mid), queryMin(i, mid)};
    long long diff = p.first - p.second;
    if (diff == 0) {
      ans = mid;
    }
    if (diff <= 0) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return ans;
}
void go() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> b[i];
  filler(n);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long r1 = bs1(i, n);
    long long r2 = bs2(i, n);
    if (r1 != -1 && r2 != -1) ans += r2 - r1 + 1;
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long t = 1;
  cout << fixed << setprecision(12);
  while (t--) {
    go();
  }
}
