#include <bits/stdc++.h>
using namespace std;
int n, a[200002], b[200002];
long long ans;
deque<int> Max, Min;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int R = 1, L = 1; R <= n; R++) {
    while (!Max.empty() && a[Max.back()] <= a[R]) Max.pop_back();
    while (!Min.empty() && b[Min.back()] >= b[R]) Min.pop_back();
    Max.push_back(R);
    Min.push_back(R);
    while (L <= R && a[Max.front()] - b[Min.front()] > 0) {
      L++;
      while (!Max.empty() && Max.front() < L) Max.pop_front();
      while (!Min.empty() && Min.front() < L) Min.pop_front();
    }
    if (!Max.empty() && !Min.empty() && a[Max.front()] == b[Min.front()])
      ans += min(Max.front(), Min.front()) - L + 1;
  }
  cout << ans;
}
