#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const long long INF = 0x7fffffffffffffff;
const long long mod = 1e9 + 7;
template <typename A, typename B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  return s << "(" << p.first << "," << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &c) {
  s << "[ ";
  for (auto it : c) s << it << " ";
  s << "]";
  return s;
}
template <typename T>
ostream &operator<<(ostream &o, const set<T> &st) {
  o << "{";
  for (auto it = st.begin(); it != st.end(); it++)
    o << (it == st.begin() ? "" : ", ") << *it;
  return o << "}";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &make_pair) {
  o << "{";
  for (auto it = make_pair.begin(); it != make_pair.end(); it++) {
    o << (it == make_pair.begin() ? "" : ", ") << it->first << ":"
      << it->second;
  }
  o << "}";
  return o;
}
struct segment_tree {
  int *arr;
  int m[4 * 200005], M[4 * 200005];
  void pull(int ind) {
    m[ind] = min(m[ind << 1 | 1], m[ind << 1]);
    M[ind] = max(M[ind << 1 | 1], M[ind << 1]);
  };
  void build(int ind, int l, int r) {
    if (r - l == 1) {
      m[ind] = M[ind] = arr[l];
      return;
    }
    int mid = (l + r) >> 1;
    build(ind << 1, l, mid);
    build(ind << 1 | 1, mid, r);
    pull(ind);
  }
  int query_min(int ind, int L, int R, int ql, int qr) {
    if (L >= qr || R <= ql) return 1e9;
    if (R <= qr && L >= ql) {
      return m[ind];
    }
    int mid = (L + R) >> 1;
    return min(query_min(ind << 1, L, mid, ql, qr),
               query_min(ind << 1 | 1, mid, R, ql, qr));
  }
  int query_max(int ind, int L, int R, int ql, int qr) {
    if (L >= qr || R <= ql) return -1e9;
    if (R <= qr && L >= ql) {
      return M[ind];
    }
    int mid = (L + R) >> 1;
    return max(query_max(ind << 1, L, mid, ql, qr),
               query_max(ind << 1 | 1, mid, R, ql, qr));
  }
} seg[2];
int a[200005], b[200005];
int n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  seg[0].arr = a;
  seg[0].build(1, 0, n);
  seg[1].arr = b;
  seg[1].build(1, 0, n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) continue;
    int ub = n + 1, lb = i;
    while (ub - lb > 1) {
      int mid = (ub + lb) >> 1;
      if (seg[0].query_max(1, 0, n, i, mid) -
              seg[1].query_min(1, 0, n, i, mid) >
          0)
        ub = mid;
      else
        lb = mid;
    }
    int up = ub;
    ub = n + 1, lb = i;
    while (ub - lb > 1) {
      int mid = (ub + lb) >> 1;
      if (seg[0].query_max(1, 0, n, i, mid) -
              seg[1].query_min(1, 0, n, i, mid) >=
          0)
        ub = mid;
      else
        lb = mid;
    }
    int down = ub;
    ans += up - down;
  }
  cout << ans << endl;
  return 0;
}
