#include <bits/stdc++.h>
int n;
int a[19][1 << 18];
int b[19][1 << 18];
using namespace std;
void Build(int a[19][1 << 18]) {
  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) {
  int res = 0;
  if (a >> 16) a >>= 16, res += 16;
  if (a >> 8) a >>= 8, res += 8;
  if (a >> 4) a >>= 4, res += 4;
  if (a >> 2) a >>= 2, res += 2;
  if (a >> 1) a >>= 1, res += 1;
  return res;
}
int Min(int a[19][1 << 18], 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]);
    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;
}
