#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B, typename C>
struct triple {
  A X;
  B Y;
  C Z;
  triple(A a = 0, B b = 0, C c = 0) : X(a), Y(b), Z(c) {}
};
template <typename A, typename B, typename C>
triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) {
  return triple<A, B, C>(a, b, c);
}
template <typename A, typename B, typename C>
bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) {
  if (a.X != b.X) return a.X < b.X;
  if (a.Y != b.Y) return a.Y < b.Y;
  return a.Z < b.Z;
}
template <typename T, typename SS>
ostream& operator<<(ostream& ofs, const pair<T, SS>& p) {
  ofs << "( " << p.first << " , " << p.second << " )";
  return ofs;
}
template <typename T>
void print(T a) {
  for (auto i : a) cout << i << ' ';
  cout << '\n';
}
template <typename T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
template <typename T>
T min(T a, T b, T c) {
  return min(a, max(b, c));
}
signed main() {
  srand(time(NULL));
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  for (auto& i : a) cin >> i;
  for (auto& i : b) cin >> i;
  deque<long long> mn, mx;
  long long ans = 0;
  for (long long i = 0, j = 0; i < n; i++) {
    while (!mx.empty() && a[mx.back()] <= a[i]) mx.pop_back();
    while (!mn.empty() && b[mn.back()] >= b[i]) mn.pop_back();
    mx.push_back(i);
    mn.push_back(i);
    while (j <= i and a[mx.front()] - b[mn.front()] > 0) {
      j++;
      while (!mx.empty() and mx.front() < j) mx.pop_front();
      while (!mn.empty() and mn.front() < j) mn.pop_front();
    }
    if (!mx.empty() && !mn.empty() && a[mx.front()] == b[mn.front()])
      ans += min(mx.front(), mn.front()) - j + 1;
  }
  cout << ans << '\n';
}
