#include <bits/stdc++.h>
using namespace std;
int n;
long long ans;
deque<int> mx, mn;
const int MAX = 200010;
const int inf = -2e9;
class Tournament {
 public:
  pair<int, int> T[(1 << 20)];
  int a[MAX];
  int b[MAX];
  int n;
  void init(int left, int right, int i) {
    if (left == right)
      T[i] = make_pair(a[left], b[left]);
    else {
      int mid = (left + right) / 2;
      init(left, mid, i * 2);
      init(mid + 1, right, i * 2 + 1);
      T[i] = combine(T[i * 2], T[i * 2 + 1]);
    }
  }
  pair<int, int> query(int left, int right) {
    return query(left, right, 1, 0, n - 1);
  }

 private:
  virtual pair<int, int> null() { return make_pair(inf, -inf); }
  virtual pair<int, int> combine(pair<int, int> val1, pair<int, int> val2) {
    return make_pair(max(val1.first, val2.first),
                     min(val1.second, val2.second));
  }
  pair<int, int> query(int left, int right, int i, int nodeL, int nodeR) {
    left = max(left, nodeL);
    right = min(right, nodeR);
    if (left == nodeL && right == nodeR) return T[i];
    if (left > right) return null();
    int mid = (nodeL + nodeR) / 2;
    return combine(query(left, right, i * 2, nodeL, mid),
                   query(left, right, i * 2 + 1, mid + 1, nodeR));
  }
};
Tournament t;
int getLeft(int i) {
  int lo = i;
  int hi = n - 1;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    pair<int, int> q = t.query(i, mid);
    int qa_ = q.first;
    int qb_ = q.second;
    if (qa_ - qb_ < 0) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }
  return lo;
}
int getRight(int i) {
  int lo = i;
  int hi = n - 1;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    pair<int, int> q = t.query(i, mid);
    int qa_ = q.first;
    int qb_ = q.second;
    if (qa_ - qb_ > 0) {
      hi = mid - 1;
    } else {
      lo = mid;
    }
  }
  return lo;
}
int main() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) scanf("%d", &t.a[i]);
  for (int i = (0); i < (n); ++i) scanf("%d", &t.b[i]);
  t.n = n;
  t.init(0, n - 1, 1);
  long long ans = 0;
  for (int i = (0); i < (n); ++i) {
    int l1 = getLeft(i);
    int l2 = getRight(i);
    pair<int, int> q = t.query(i, l1);
    if ((l1 <= l2) and (q.first - q.second == 0)) {
      ans += (l2 - l1 + 1);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
