#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline istream &operator>>(istream &fin, pair<T1, T2> &pr) {
  fin >> pr.first >> pr.second;
  return fin;
}
template <class T0, class T1, class T2>
inline istream &operator>>(istream &fin, tuple<T0, T1, T2> &t) {
  fin >> get<0>(t) >> get<1>(t) >> get<2>(t);
  return fin;
}
template <class T>
inline istream &operator>>(istream &fin, vector<T> &a) {
  for (auto &u : a) fin >> u;
  return fin;
}
template <class T, size_t n>
inline istream &operator>>(istream &fin, array<T, n> &a) {
  for (auto &u : a) fin >> u;
  return fin;
}
template <class FwdIter>
inline auto dump(FwdIter first, FwdIter last, const char *dlm) -> void {
  copy(first, last,
       ostream_iterator<typename iterator_traits<FwdIter>::value_type>(cout,
                                                                       dlm));
}
template <class value_t, class func_t>
class SparseTable {
 public:
  SparseTable(const vector<value_t> &a, func_t f)
      : fn(f), st(vector<vector<value_t> >(a.size())) {
    const auto n = a.size();
    for (size_t i = 0; i < n; ++i) st[i].emplace_back(a[i]);
    for (size_t stride = 2; stride < n + 1; stride <<= 1) {
      const auto shift = stride >> 1;
      for (size_t i = 0; i + stride < n + 1; ++i)
        st[i].emplace_back(fn(st[i].back(), st[i + shift].back()));
    }
  }
  inline value_t operator()(const size_t first, const size_t last) const {
    size_t k(0), leap(last - first + 1);
    while (leap >>= 1) ++k;
    const size_t i = last + 1 - (size_t)(1 << k);
    return fn(st[first][k], st[i][k]);
  }

 private:
  const func_t fn;
  vector<vector<value_t> > st;
};
int64_t d689() {
  size_t n;
  cin >> n;
  vector<int> a(n), b(n);
  cin >> a >> b;
  typedef const int &(*func_t)(const int &, const int &);
  SparseTable<int, func_t> x(a, max), y(b, min);
  int64_t out = 0;
  for (size_t i = 0; i < n; ++i) {
    size_t lo = i - 1, hi = n;
    while (lo + 1 < hi) {
      const auto j = (lo + hi) >> 1;
      (y(i, j) < x(i, j) ? hi : lo) = j;
    }
    const auto k = lo;
    lo = i - 1;
    hi = k + 1;
    while (lo + 1 < hi) {
      const auto j = (lo + hi) >> 1;
      (x(i, j) < y(i, j) ? lo : hi) = j;
    }
    out += k - lo;
  }
  return out;
}
int main(const int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << d689();
  return EXIT_SUCCESS;
}
