#include <bits/stdc++.h>
using namespace std;
int M[200050][25] = {0};
int A[200050];
int NO[200050][25] = {INT_MAX};
int B[200050];
int N;
int minidx, maxidx;
void process1() {
  int i, j;
  for (i = 0; i < N; i++) M[i][0] = i;
  for (j = 1; 1 << j <= N; j++)
    for (i = 0; i + (1 << j) - 1 < N; i++)
      if (A[M[i][j - 1]] > A[M[i + (1 << (j - 1))][j - 1]])
        M[i][j] = M[i][j - 1];
      else
        M[i][j] = M[i + (1 << (j - 1))][j - 1];
}
int RMQ1(int i, int j) {
  int k = (log2(j - i + 1));
  return max(A[M[i][k]], A[M[j - (1 << k) + 1][k]]);
}
int RMQ2(int i, int j) {
  int k = (log2(j - i + 1));
  return min(B[NO[i][k]], B[NO[j - (1 << k) + 1][k]]);
}
void process2() {
  int i, j;
  for (i = 0; i < N; i++) NO[i][0] = i;
  for (j = 1; 1 << j <= N; j++)
    for (i = 0; i + (1 << j) - 1 < N; i++)
      if (B[NO[i][j - 1]] < B[NO[i + (1 << (j - 1))][j - 1]])
        NO[i][j] = NO[i][j - 1];
      else
        NO[i][j] = NO[i + (1 << (j - 1))][j - 1];
}
void rec_bin1(int lo, int hi, int i) {
  int mid;
  while (lo <= hi) {
    mid = lo + (hi - lo) / 2;
    if (RMQ1(i, mid) - RMQ2(i, mid) == 0) {
      maxidx = max(mid, maxidx);
      minidx = min(minidx, mid);
    }
    if (RMQ1(i, mid) - RMQ2(i, mid) <= 0)
      lo = mid + 1;
    else
      hi = mid - 1;
  }
}
void rec_bin2(int lo, int hi, int i) {
  int mid;
  while (lo <= hi) {
    mid = lo + (hi - lo) / 2;
    if (RMQ1(i, mid) - RMQ2(i, mid) == 0) {
      maxidx = max(mid, maxidx);
      minidx = min(minidx, mid);
    }
    if (RMQ1(i, mid) - RMQ2(i, mid) < 0)
      lo = mid + 1;
    else
      hi = mid - 1;
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) cin >> B[i];
  process1();
  process2();
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    minidx = INT_MAX;
    maxidx = -1;
    rec_bin1(i, N - 1, i);
    rec_bin2(i, N - 1, i);
    if (maxidx != -1) ans = ans + maxidx - minidx + 1;
  }
  cout << ans << endl;
}
