#include <bits/stdc++.h>
using namespace std;
int n, d, lg[200100] = {-1};
long long ans;
int maxa[200100][30], minb[200100][30];
int getmax(int x, int y) {
  int k = lg[y - x + 1];
  return max(maxa[x][k], maxa[y + 1 - (1 << k)][k]);
}
int getmin(int x, int y) {
  int k = lg[y - x + 1];
  return min(minb[x][k], minb[y + 1 - (1 << k)][k]);
}
int lower(int x, int l, int r) {
  int mid;
  while (r > l) {
    mid = (l + r) / 2;
    if (getmax(x, mid) >= getmin(x, mid))
      r = mid;
    else
      l = mid + 1;
  }
  if (getmax(x, l) == getmin(x, l))
    return l;
  else
    return 0;
}
int upper(int x, int l, int r) {
  int mid;
  while (r > l) {
    mid = (l + r + 1) / 2;
    if (getmax(x, mid) <= getmin(x, mid))
      l = mid;
    else
      r = mid - 1;
  }
  return l;
}
int main() {
  int i, j, l, r, t;
  cin >> n;
  for (i = 1; i < 200100; i++) lg[i] = lg[i / 2] + 1;
  for (i = 1; i <= n; i++) cin >> maxa[i][0];
  for (i = 1; i <= n; i++) cin >> minb[i][0];
  for (j = 1; j <= lg[n]; j++) {
    t = n - (1 << j) + 1;
    for (i = 1; i <= t; i++) {
      maxa[i][j] = max(maxa[i][j - 1], maxa[i + (1 << (j - 1))][j - 1]);
      minb[i][j] = min(minb[i][j - 1], minb[i + (1 << (j - 1))][j - 1]);
    }
  }
  for (i = 1; i <= n; i++)
    if (minb[i][0] >= maxa[i][0]) {
      l = lower(i, i, n);
      if (l > 0) {
        r = upper(i, i, n);
        ans += r - l + 1;
      }
    }
  cout << ans << endl;
}
