#include <bits/stdc++.h>
using namespace std;
int n, a[200010], b[200010];
int maxa[200010][20], minb[200010][20];
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  for (int i = 0; i < n; i++) {
    maxa[i][0] = a[i];
    minb[i][0] = b[i];
  }
  for (int i = 1; i < 20; i++) {
    for (int j = 0; j < n; j++) {
      maxa[j][i] = maxa[j][i - 1];
      minb[j][i] = minb[j][i - 1];
      if (j + (1 << (i - 1)) < n) {
        maxa[j][i] = max(maxa[j][i], maxa[j + (1 << (i - 1))][i - 1]);
        minb[j][i] = min(minb[j][i], minb[j + (1 << (i - 1))][i - 1]);
      }
    }
  }
  long long total = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) continue;
    int lbound = i, ubound = i;
    int cmax = -2e9, cmin = 2e9;
    for (int j = 19; j >= 0; j--) {
      if (max(cmax, maxa[lbound][j]) < min(cmin, minb[lbound][j])) {
        cmax = max(cmax, maxa[lbound][j]);
        cmin = min(cmin, minb[lbound][j]);
        lbound += 1 << j;
        if (lbound >= n) break;
      }
    }
    cmax = a[i], cmin = b[i];
    for (int j = 19; j >= 0; j--) {
      if (max(cmax, maxa[ubound][j]) <= min(cmin, minb[ubound][j])) {
        cmax = max(cmax, maxa[ubound][j]);
        cmin = min(cmin, minb[ubound][j]);
        ubound += 1 << j;
        if (ubound >= n) break;
      }
    }
    total += min(ubound, n) - min(lbound, n);
  }
  cout << total << "\n";
  return 0;
}
