#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N], b[N];
int d1[N][25], d2[N][25];
void RMQ1_init(int *A, int n) {
  for (int i = 0; i < n; i++) d1[i][0] = A[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      d1[i][j] = max(d1[i][j - 1], d1[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int RMQ1(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return max(d1[L][k], d1[R - (1 << k) + 1][k]);
}
void RMQ2_init(int *A, int n) {
  for (int i = 0; i < n; i++) d2[i][0] = A[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      d2[i][j] = min(d2[i][j - 1], d2[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int RMQ2(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return min(d2[L][k], d2[R - (1 << k) + 1][k]);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  RMQ1_init(a, n);
  RMQ2_init(b, n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int l = i, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      if (RMQ1(i, mid) < RMQ2(i, mid))
        l = mid + 1;
      else
        r = mid;
    }
    int p1 = l;
    l = i, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      if (RMQ1(i, mid) <= RMQ2(i, mid))
        l = mid + 1;
      else
        r = mid;
    }
    int p2 = l;
    sum += p2 - p1;
  }
  cout << sum << endl;
}
