#include <bits/stdc++.h>
using namespace std;
template <class T>
int getBit(T x, int k) {
  return x >> k & 1;
}
template <class T>
T onBit(T x, int k) {
  return x(T(1) << k);
}
template <class T>
T offBit(T x, int k) {
  return x & (~(T(1) << k));
}
template <class T>
void read(T &x) {
  x = 0;
  char c;
  bool nega = 0;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  if (c == '-') nega = 1, c = getchar();
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  if (nega) x = -x;
}
template <class T>
void wri(T x) {
  if (x / 10) wri(x / 10);
  putchar(x % 10 + '0');
}
template <class T>
void write(T x) {
  if (x < 0) putchar('-');
  wri(abs(x));
}
template <class T>
void writeln(T x) {
  write(x);
  putchar('\n');
}
const int N = 2e5 + 10;
int n;
int a[N], b[N], rmq1[22][N], rmq2[22][N];
void load() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    read(b[i]);
  }
}
int get1(int u, int v) {
  int k = log2(v - u + 1);
  return max(rmq1[k][u], rmq1[k][v - (1 << k) + 1]);
}
int get2(int u, int v) {
  int k = log2(v - u + 1);
  return min(rmq2[k][u], rmq2[k][v - (1 << k) + 1]);
}
void process() {
  for (int i = 1; i <= n; ++i) rmq1[0][i] = a[i], rmq2[0][i] = b[i];
  for (int j = 1; j <= 20; ++j)
    for (int i = 1; i <= n - (1 << j) + 1; ++i) {
      rmq1[j][i] = max(rmq1[j - 1][i], rmq1[j - 1][i + (1 << (j - 1))]);
      rmq2[j][i] = min(rmq2[j - 1][i], rmq2[j - 1][i + (1 << (j - 1))]);
    }
  long long ans = 0;
  int p = 1, q = 1;
  for (int i = 1; i <= n; ++i) {
    while (p < i) p++;
    while (q < i) q++;
    while (p <= n && get1(i, p) <= get2(i, p)) p++;
    while (q <= n & get1(i, q) < get2(i, q)) q++;
    ans += p - q;
  }
  writeln(ans);
}
int main() {
  load();
  process();
  return 0;
}
