#include <bits/stdc++.h>
int n;
int a[19][200000];
int b[19][200000];
int lz[1 << 8];
using namespace std;
void Build(int a[19][200000]) {
  for (int len = 1, d = 1; len <= n; len += len, d++)
    for (int i = 0; i < n; i++)
      a[d][i] = min(a[d - 1][i], a[d - 1][i + len >= n ? i : i + len]);
}
int Scnt(int a) {
  union q {
    int u;
    unsigned char b[4];
  } q;
  q.u = a;
  if (q.b[2]) return lz[q.b[2]] + 16;
  if (q.b[1]) return lz[q.b[1]] + 8;
  return lz[q.b[0]];
}
int Min(int a[19][200000], int l, int r) {
  int len = r - l;
  if (len == 0) return 0;
  int s = Scnt(len);
  int len2 = 1 << s;
  return min(a[s][l], a[s][r - len2]);
}
int Brute() {
  int c = 0;
  for (int l = 0; l < n; l++)
    for (int r = l + 1; r <= n; r++)
      if (-Min(a, l, r) == Min(b, l, r)) ++c;
  return c;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[0][i]);
    a[0][i] = -a[0][i];
  }
  for (int i = 0; i < n; i++) scanf("%d", &b[0][i]);
  int p = 1;
  int z = 0;
  for (int i = 0; i < (1 << 8); i++) {
    if (i == p) p += p, ++z;
    lz[i] = z - 1;
  }
  Build(a);
  Build(b);
  int d = 0;
  while ((1 << d) <= n) ++d;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    int l = i, r = n;
    while (l < r) {
      int m = (l + r + 1) >> 1;
      if (-Min(a, i, m) < Min(b, i, m))
        l = m;
      else
        r = m - 1;
    }
    if (l == n) continue;
    int left = l;
    r = n;
    while (l < r) {
      int m = (l + r + 1) >> 1;
      if (-Min(a, i, m) == Min(b, i, m))
        l = m;
      else
        r = m - 1;
    }
    res += r - left;
  }
  cout << res << endl;
  return 0;
}
