#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 100;
const int Log = 30;
long long n, ans;
int stmax[maxn][Log], stmin[maxn][Log], mn[maxn];
int numa[maxn], numb[maxn];
void Init() {
  mn[0] = -1;
  for (int i = 1; i <= n; ++i) {
    mn[i] = ((i & (i - 1)) == 0) ? mn[i - 1] + 1 : mn[i - 1];
    stmax[i][0] = numa[i];
    stmin[i][0] = numb[i];
  }
  for (int j = 1; j <= mn[n]; ++j) {
    for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
      stmax[i][j] = max(stmax[i][j - 1], stmax[i + (1 << (j - 1))][j - 1]);
      stmin[i][j] = min(stmin[i][j - 1], stmin[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int rmq_max(int L, int R) {
  int k = mn[R - L + 1];
  return max(stmax[L][k], stmax[R - (1 << k) + 1][k]);
}
int rmq_min(int L, int R) {
  int k = mn[R - L + 1];
  return min(stmin[L][k], stmin[R - (1 << k) + 1][k]);
}
int get_r1(int l) {
  int high = n;
  int low = l - 1;
  int mid;
  while (high - low > 1) {
    mid = (high + low) / 2;
    if (rmq_max(l, mid) - rmq_min(l, mid) >= 0) {
      high = mid;
    } else {
      low = mid;
    }
  }
  if (rmq_max(l, high) - rmq_min(l, high) < 0) {
    return high + 1;
  }
  return high;
}
int get_r2(int l) {
  int high = n;
  int low = l - 1;
  int mid;
  while (high - low > 1) {
    mid = (low + high) / 2;
    if (rmq_max(l, mid) - rmq_min(l, mid) > 0) {
      high = mid;
    } else {
      low = mid;
    }
  }
  if (rmq_max(l, high) - rmq_min(l, high) <= 0) {
    return high + 1;
  }
  return high;
}
int main() {
  ios::sync_with_stdio(false);
  scanf("%I64d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &numa[i]);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &numb[i]);
  }
  Init();
  for (int l = 1; l <= n; ++l) {
    int r1 = get_r1(l);
    int r2 = get_r2(l);
    ans += r2 - r1;
  }
  printf("%I64d\n", ans);
  return 0;
}
