#include <bits/stdc++.h>
using namespace std;
int n, st1[200010][33], st0[200010][33];
long long sum = 0;
int qmax(int l, int r) {
  if (l == r) return st1[l][0];
  int k = log2(r - l + 1);
  return max(st1[l][k], st1[r - (1 << k) + 1][k]);
}
int qmin(int l, int r) {
  if (l == r) return st0[l][0];
  int k = log2(r - l + 1);
  return min(st0[l][k], st0[r - (1 << k) + 1][k]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &st1[i][0]);
  for (int i = 1; i <= n; i++) scanf("%d", &st0[i][0]);
  for (int j = 1; j <= 20; j++)
    for (int i = 1; i <= n; i++) {
      if (i + (1 << (j - 1)) > n) break;
      st0[i][j] = min(st0[i][j - 1], st0[i + (1 << (j - 1))][j - 1]);
      st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]);
    }
  for (int i = 1; i <= n; i++) {
    int l = i, r = n, mid, x0, x1;
    while (l < r) {
      mid = (l + r) / 2;
      if (qmax(i, mid) >= qmin(i, mid))
        r = mid;
      else
        l = mid + 1;
    }
    x0 = r;
    if (qmax(i, x0) > qmin(i, x0)) continue;
    r = n;
    l = x0;
    if (qmax(i, n) == qmin(i, n))
      x1 = n;
    else {
      while (l < r) {
        mid = (l + r) / 2;
        if (qmax(i, mid) > qmin(i, mid))
          r = mid;
        else
          l = mid + 1;
      }
      x1 = r - 1;
    }
    sum += x1 - x0 + 1;
  }
  printf("%lld", sum);
  return 0;
}
