#include <bits/stdc++.h>
using namespace std;
int n, a[200001], b[200001];
long long ans;
deque<int> mxx, mnn;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1, j = 1; i <= n; i++) {
    while (!mxx.empty() and a[mxx.back()] <= a[i]) mxx.pop_back();
    mxx.push_back(i);
    while (!mnn.empty() and b[mnn.back()] >= b[i]) mnn.pop_back();
    mnn.push_back(i);
    while (j <= i and a[mxx.front()] > b[mnn.front()]) {
      j++;
      while (!mxx.empty() and mxx.front() < j) mxx.pop_front();
      while (!mnn.empty() and mnn.front() < j) mnn.pop_front();
    }
    if (!mxx.empty() and !mnn.empty() and a[mxx.front()] == b[mnn.front()])
      ans += min(mxx.front(), mnn.front()) - j + 1;
  }
  printf("%I64d", ans);
}
