#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7, mod1 = 1e9 + 9;
const long long N = 200005, LN = 18;
long long logs[N], mx[N][20], mn[N][20], a[N], b[N];
long long n;
void pre() {
  logs[1] = 0;
  for (long long i = 2; i <= (n); i++) {
    logs[i] = logs[i >> 1] + 1;
  }
  for (long long i = 1; i <= (n); i++) mx[i][0] = a[i];
  for (long long i = 1; i <= (n); i++) mn[i][0] = b[i];
  for (long long j = 1; j <= (18); j++) {
    for (long long i = 1; i <= (n); i++) {
      if ((i + (1 << j) - 1) > n) break;
      mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
      mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
    }
  }
}
long long calmax(long long l, long long r) {
  long long k = logs[r - l + 1];
  return max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
long long calmin(long long l, long long r) {
  long long k = logs[r - l + 1];
  return min(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  for (long long i = 1; i <= (n); i++) cin >> a[i];
  for (long long i = 1; i <= (n); i++) cin >> b[i];
  pre();
  long long ans = 0;
  for (long long i = 1; i <= (n); i++) {
    long long lo = i, hi = n;
    while (lo < hi) {
      long long mi = lo + hi >> 1;
      if (calmax(i, mi) < calmin(i, mi))
        lo = mi + 1;
      else
        hi = mi;
    }
    if (calmax(i, lo) != calmin(i, lo)) continue;
    long long tm = lo;
    lo = i, hi = n;
    while (lo < hi) {
      long long mi = lo + hi + 1 >> 1;
      if (calmax(i, mi) > calmin(i, mi))
        hi = mi - 1;
      else
        lo = mi;
    }
    ans += (lo - tm + 1);
  }
  cout << ans;
  return 0;
}
