#include <bits/stdc++.h>
using namespace std;
class SparseTableRMQmin {
  int maxlog;
  std::vector<char> log;
  std::vector<int> M;

 public:
  SparseTableRMQmin(const std::vector<int> &a) {
    int n = a.size();
    log.resize(n + 1);
    log[0] = 0;
    for (int i = 1, a = 0; i <= n; i++) {
      if ((1 << a + 1) < i) a++;
      log[i] = a;
    }
    maxlog = log[n] + 1;
    M.resize(n * maxlog);
    for (int i = 0; i < n; i++) M[i * maxlog] = a[i];
    for (int j = 1; 1 << j <= n; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        M[i * maxlog + j] = std::min(M[i * maxlog + j - 1],
                                     M[(i + (1 << j - 1)) * maxlog + j - 1]);
      }
    }
  }
  int query(int i, int j) const {
    int k = log[j - i];
    return std::min(M[i * maxlog + k], M[(j - (1 << k)) * maxlog + k]);
  }
};
class SparseTableRMQmax {
  int maxlog;
  std::vector<char> log;
  std::vector<int> M;

 public:
  SparseTableRMQmax(const std::vector<int> &a) {
    int n = a.size();
    log.resize(n + 1);
    log[0] = 0;
    for (int i = 1, a = 0; i <= n; i++) {
      if ((1 << a + 1) < i) a++;
      log[i] = a;
    }
    maxlog = log[n] + 1;
    M.resize(n * maxlog);
    for (int i = 0; i < n; i++) M[i * maxlog] = a[i];
    for (int j = 1; 1 << j <= n; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        M[i * maxlog + j] = std::max(M[i * maxlog + j - 1],
                                     M[(i + (1 << j - 1)) * maxlog + j - 1]);
      }
    }
  }
  int query(int i, int j) const {
    int k = log[j - i];
    return std::max(M[i * maxlog + k], M[(j - (1 << k)) * maxlog + k]);
  }
};
int n;
int search(SparseTableRMQmax &a, SparseTableRMQmin &b, int p, int v) {
  int l = p, r = n + 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (a.query(p, m) - b.query(p, m) >= v) {
      r = m;
    } else {
      l = m;
    }
  }
  return r;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < (int)(n); i++) cin >> a[i];
  for (int i = 0; i < (int)(n); i++) cin >> b[i];
  SparseTableRMQmax ar(a);
  SparseTableRMQmin br(b);
  long long ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    if (a[i] > b[i]) continue;
    ans += search(ar, br, i, 1) - search(ar, br, i, 0);
  }
  cout << ans << endl;
  return 0;
}
