#include <bits/stdc++.h>
using namespace std;
const int INF = 1500000000;
const int MOD = 1000000007;
const int HASH = 137;
int N, A[200005], B[200005], STmax[20][200005], STmin[20][200005];
inline void SparseTableMax() {
  for (int i = 0; i < N; i++) STmax[0][i] = A[i];
  for (int i = 1; (1 << i) <= N; i++)
    for (int j = 0; j + (1 << i) <= N; j++)
      STmax[i][j] = max(STmax[i - 1][j], STmax[i - 1][j + (1 << (i - 1))]);
}
inline void SparseTableMin() {
  for (int i = 0; i < N; i++) STmin[0][i] = B[i];
  for (int i = 1; (1 << i) <= N; i++)
    for (int j = 0; j + (1 << i) <= N; j++)
      STmin[i][j] = min(STmin[i - 1][j], STmin[i - 1][j + (1 << (i - 1))]);
}
int QueryMax(int a, int b) {
  if (a > b) swap(a, b);
  int p = 31 - __builtin_clz(b - a + 1);
  return max(STmax[p][a], STmax[p][b - (1 << p) + 1]);
}
int QueryMin(int a, int b) {
  if (a > b) swap(a, b);
  int p = 31 - __builtin_clz(b - a + 1);
  return min(STmin[p][a], STmin[p][b - (1 << p) + 1]);
}
int LB(int s) {
  int l = s, r = N - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (QueryMax(s, mid) >= QueryMin(s, mid))
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}
int UB(int s) {
  int l = s, r = N - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (QueryMax(s, mid) > QueryMin(s, mid))
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) cin >> B[i];
  SparseTableMax();
  SparseTableMin();
  long long Ans = 0;
  for (int i = 0; i < N - 1; i++) {
    int U = UB(i);
    int L = LB(i);
    if (QueryMax(i, U) == QueryMin(i, U)) U++;
    Ans += U - L;
  }
  if (A[N - 1] == B[N - 1]) Ans++;
  cout << Ans << endl;
  return 0;
}
