#include <bits/stdc++.h>
int n;
int A[200005], B[200005];
long long ans = 0;
int d_min[200005][25], d_max[200005][25];
void RMQ_init() {
  for (int i = 1; i <= n; i++) {
    d_min[i][0] = B[i];
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      d_min[i][j] = std::min(d_min[i][j - 1], d_min[i + (1 << (j - 1))][j - 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    d_max[i][0] = A[i];
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      d_max[i][j] = std::max(d_max[i][j - 1], d_max[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int Get1(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return std::max(d_max[L][k], d_max[R - (1 << k) + 1][k]);
}
int Get2(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return std::min(d_min[L][k], d_min[R - (1 << k) + 1][k]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &B[i]);
  RMQ_init();
  int L, R;
  for (int i = 1; i <= n; i++) {
    if (A[i] > B[i]) continue;
    int l = i, r = n + 1;
    while (r - l > 1) {
      int mid = (l + r) >> 1;
      if (Get2(i, mid) >= Get1(i, mid))
        l = mid;
      else
        r = mid;
    }
    R = l;
    if (Get2(i, n) > Get1(i, n)) continue;
    l = i - 1, r = n;
    while (r - l > 1) {
      int mid = (l + r) >> 1;
      if (Get1(i, mid) >= Get2(i, mid))
        r = mid;
      else
        l = mid;
    }
    L = r;
    ans = ans + R - L + 1;
  }
  printf("%I64d\n", ans);
  return 0;
}
