#include <bits/stdc++.h>
using LL = long long;
const int INF = 0x3f3f3f3f;
const int N = 200000 + 5;
int n, A[N], B[N];
int C[N], D[N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", A + i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", B + i);
  }
  std::fill(C, C + n, -INF);
  std::fill(D, D + n, INF);
  LL result = 0;
  for (int i = n - 1; i >= 0; --i) {
    for (int at = i; at < n; at += ~at & at + 1) {
      C[at] = std::max(C[at], A[i]);
      D[at] = std::min(D[at], B[i]);
    }
    int l = -1, r = -1, c = -INF, d = INF;
    for (int step = 20; step >= 0; --step) {
      l += 1 << step;
      if (l >= n || std::max(c, C[l]) >= std::min(d, D[l]))
        l -= 1 << step;
      else
        c = std::max(c, C[l]), d = std::min(d, D[l]);
    }
    c = -INF, d = INF;
    for (int step = 20; step >= 0; --step) {
      r += 1 << step;
      if (r >= n || std::max(c, C[r]) > std::min(d, D[r]))
        r -= 1 << step;
      else
        c = std::max(c, C[r]), d = std::min(d, D[r]);
    }
    result += r - l;
  }
  printf("%I64d\n", result);
}
