#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int LOG = 18;
int n, mx[MAXN], mn[MAXN], a[MAXN], b[MAXN];
inline void update_max(int pos, int val) {
  for (; pos <= n; pos += (pos & (-pos))) mx[pos] = max(mx[pos], val);
}
inline void update_min(int pos, int val) {
  for (; pos <= n; pos += (pos & (-pos))) mn[pos] = min(mn[pos], val);
}
inline int query_max(int pos) {
  int ans = -INF;
  for (; pos > 0; pos -= (pos & (-pos))) ans = max(ans, mx[pos]);
  return ans;
}
inline int query_min(int pos) {
  int ans = INF;
  for (; pos > 0; pos -= (pos & (-pos))) ans = min(ans, mn[pos]);
  return ans;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) mx[i] = -INF, mn[i] = INF;
  long long answer = 0, left, right;
  for (int i = n; i >= 1; i--) {
    update_max(i, a[i]);
    update_min(i, b[i]);
    if (a[i] > b[i]) continue;
    left = i - 1;
    right = i;
    for (int step = (1 << LOG); step >= 1; step >>= 1) {
      if (left + step <= n && query_max(left + step) < query_min(left + step))
        left += step;
      if (right + step <= n &&
          query_max(right + step) <= query_min(right + step))
        right += step;
    }
    answer += right - left;
  }
  cout << answer;
  return 0;
}
