#include <bits/stdc++.h>
using namespace std;
int a[200010];
int b[200010];
int dp_max[200010][20];
int dp_min[200010][20];
int n;
int res;
int Max(int a, int b) { return a > b ? a : b; }
int Min(int a, int b) { return a < b ? a : b; }
void rmq_init() {
  for (int i = 0; i < n; i++) {
    dp_max[i][0] = a[i];
    dp_min[i][0] = b[i];
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      dp_max[i][j] = max(dp_max[i][j - 1], dp_max[i + (1 << (j - 1))][j - 1]);
      dp_min[i][j] = min(dp_min[i][j - 1], dp_min[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int rmq_max(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return max(dp_max[L][k], dp_max[R - (1 << k) + 1][k]);
}
int rmq_min(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return min(dp_min[L][k], dp_min[R - (1 << k) + 1][k]);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  rmq_init();
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int l = i, r = n - 1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (rmq_max(i, mid) - rmq_min(i, mid) < 0)
        l = mid + 1;
      else
        r = mid - 1;
    }
    ans -= l;
    l = i, r = n - 1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (rmq_max(i, mid) - rmq_min(i, mid) <= 0)
        l = mid + 1;
      else
        r = mid - 1;
    }
    ans += l;
  }
  cout << ans << endl;
  return 0;
}
