#include <bits/stdc++.h>
using namespace std;
long long int dpmax[200005][20];
long long int dpmin[200005][20];
long long int a[200005], b[200005];
int n;
void ST() {
  for (int i = 1; i <= n; i++) {
    dpmax[i][0] = a[i];
    dpmin[i][0] = b[i];
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      dpmax[i][j] = dpmax[i][j - 1] > dpmax[i + (1 << (j - 1))][j - 1]
                        ? dpmax[i][j - 1]
                        : dpmax[i + (1 << (j - 1))][j - 1];
      dpmin[i][j] = dpmin[i][j - 1] < dpmin[i + (1 << (j - 1))][j - 1]
                        ? dpmin[i][j - 1]
                        : dpmin[i + (1 << (j - 1))][j - 1];
    }
  }
}
long long int RMQmax(int l, int r) {
  int k = 0;
  while ((1 << (k + 1)) <= r - l + 1) k++;
  return dpmax[l][k] > dpmax[r - (1 << k) + 1][k] ? dpmax[l][k]
                                                  : dpmax[r - (1 << k) + 1][k];
}
long long int RMQmin(int l, int r) {
  int k = 0;
  while ((1 << (k + 1)) <= r - l + 1) k++;
  return dpmin[l][k] < dpmin[r - (1 << k) + 1][k] ? dpmin[l][k]
                                                  : dpmin[r - (1 << k) + 1][k];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  ST();
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    int l = i, r = n, mid, flag = 0;
    long long int x, y;
    long long int last = 0, first = 0;
    while (l <= r) {
      mid = (l + r) / 2;
      x = RMQmax(i, mid);
      y = RMQmin(i, mid);
      if (x == y) {
        flag = 1;
        last = mid;
      }
      if (x > y) r = mid - 1;
      if (x < y || x == y) l = mid + 1;
    }
    if (flag) {
      l = i;
      r = n;
      while (l <= r) {
        mid = (l + r) / 2;
        x = RMQmax(i, mid);
        y = RMQmin(i, mid);
        if (x < y) {
          l = mid + 1;
        } else {
          r = mid - 1;
          first = mid;
        }
      }
      ans += (last - first + 1);
    }
  }
  cout << ans << '\n';
  return 0;
}
