#include <bits/stdc++.h>
using namespace std;
int lookup1[200010][20], lookup2[200010][20];
int arr1[200010], arr2[200010];
int n;
class SparseMin {
 public:
  SparseMin(void) {}
  void build(void) {
    for (int i = 0; i < n; i++) {
      lookup1[i][0] = i;
    }
    for (int j = 1; (1 << j) <= n; j++) {
      for (int i = 0; i + (1 << j) <= n; i++) {
        if (arr1[lookup1[i][j - 1]] < arr1[lookup1[i + (1 << j - 1)][j - 1]])
          lookup1[i][j] = lookup1[i][j - 1];
        else
          lookup1[i][j] = lookup1[i + (1 << j - 1)][j - 1];
      }
    }
  }
  int query(int left, int right) {
    int len = sizeof(int) * 8 - __builtin_clz(right - left + 1);
    return min(arr1[lookup1[left][len - 1]],
               arr1[lookup1[right - (1 << (len - 1)) + 1][len - 1]]);
  }
};
class SparseMax {
 public:
  SparseMax(void) {}
  void build(void) {
    for (int i = 0; i < n; i++) {
      lookup2[i][0] = i;
    }
    for (int j = 1; (1 << j) <= n; j++) {
      for (int i = 0; i + (1 << j) <= n; i++) {
        if (arr2[lookup2[i][j - 1]] > arr2[lookup2[i + (1 << j - 1)][j - 1]])
          lookup2[i][j] = lookup2[i][j - 1];
        else
          lookup2[i][j] = lookup2[i + (1 << j - 1)][j - 1];
      }
    }
  }
  int query(int left, int right) {
    int len = sizeof(int) * 8 - __builtin_clz(right - left + 1);
    return max(arr2[lookup2[left][len - 1]],
               arr2[lookup2[right - (1 << (len - 1)) + 1][len - 1]]);
  }
};
SparseMax p1;
SparseMin p2;
int bins1(int i, int left, int right) {
  if (left > right) return right;
  int mid = (left + right) >> 1;
  int x = p1.query(i, mid), y = p2.query(i, mid);
  if (x <= y)
    return bins1(i, mid + 1, right);
  else if (x > y)
    return bins1(i, left, mid - 1);
}
int bins2(int i, int left, int right) {
  if (left > right) return right;
  int mid = (left + right) >> 1;
  int x = p1.query(i, mid), y = p2.query(i, mid);
  if (x < y)
    return bins2(i, mid + 1, right);
  else if (x >= y)
    return bins2(i, left, mid - 1);
}
int main() {
  int u, v;
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &arr2[i]);
  for (int i = 0; i < n; i++) scanf("%d", &arr1[i]);
  p1.build();
  p2.build();
  for (int i = 0; i < n; i++) {
    u = bins1(i, i, n - 1);
    v = bins2(i, i, n - 1);
    ans += max(0, u - v);
  }
  printf("%I64d\n", ans);
  return 0;
}
