#include <bits/stdc++.h>
using namespace std;
int a[301000], b[301000], c[301000];
int dp[301000][50], d[301000][50], n;
void RMQ_init() {
  for (int i = 1; i <= n; i++) dp[i][0] = a[i];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
int RMQ(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return max(dp[L][k], dp[R - (1 << k) + 1][k]);
}
void rmq_init() {
  for (int i = 1; i <= n; i++) d[i][0] = b[i];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
}
int rmq(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return min(d[L][k], d[R - (1 << k) + 1][k]);
}
struct node {
  int l, r, x;
} D[301000];
bool cmp(node a, node b) {
  if (a.x == b.x) return a.r < b.r;
  return a.x < b.x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  rmq_init();
  RMQ_init();
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int l = i, r = n, l1 = i, r1 = i - 1;
    while (l <= r) {
      int mid = (l + r) >> 1;
      int xx = rmq(i, mid), yy = RMQ(i, mid);
      if (xx > yy)
        l = mid + 1;
      else {
        r = mid - 1;
        if (xx == yy) l1 = mid;
      }
    }
    l = i, r = n;
    while (l <= r) {
      int mid = (l + r) >> 1;
      int xx = rmq(i, mid), yy = RMQ(i, mid);
      if (xx >= yy) {
        l = mid + 1;
        if (xx == yy) r1 = mid;
      } else
        r = mid - 1;
    }
    if (r1 >= l1) ans = ans + (r1 - l1 + 1);
  }
  cout << ans << '\n';
  return 0;
}
