#include <bits/stdc++.h>
using namespace std;
const int INFint = 2e9;
const long long INF = 2e18;
const int MOD = 1e9 + 7;
const int MOD1 = 1e9 + 33;
class SegmentTree {
 public:
  int n;
  vector<int> t;
  int type;
  int comb(int a, int b) { return type ? max(a, b) : min(a, b); }
  void build(vector<int> &v) {
    for (int i = 0; i < n; i++) t[i + n] = v[i];
    for (int i = n - 1; i > 0; i--) t[i] = comb(t[i << 1], t[i << 1 | 1]);
  }
  int query(int l, int r) {
    int res = type ? -INFint : INFint;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res = comb(res, t[l++]);
      if (r & 1) res = comb(res, t[--r]);
    }
    return res;
  }
  SegmentTree(int n, int type) : n(n), type(type) {
    if (type == 1)
      t.resize(n << 1, -INFint);
    else
      t.resize(n << 1, INFint);
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto &x : a) cin >> x;
  for (auto &x : b) cin >> x;
  SegmentTree sgmin(n, 0), sgmax(n, 1);
  sgmin.build(b);
  sgmax.build(a);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int l = i, r = n - 1;
    int left = -1;
    while (l <= r) {
      int md = (l + r) / 2;
      if (sgmax.query(i, md + 1) >= sgmin.query(i, md + 1)) {
        r = md - 1;
        left = md;
      } else {
        l = md + 1;
      }
    }
    if (left == -1) continue;
    int right = -1;
    l = left, r = n - 1;
    while (l <= r) {
      int md = (l + r) / 2;
      if (sgmax.query(i, md + 1) == sgmin.query(i, md + 1)) {
        l = md + 1;
        right = md;
      } else {
        r = md - 1;
      }
    }
    if (right != -1) ans += right - left + 1;
  }
  cout << ans;
  fprintf(stderr, "\nTIME = %lf\n", 1.0 * clock() / CLOCKS_PER_SEC);
  ;
  return 0;
}
