#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 200010;
const int MAX = 1000000100;
const int mod = 100000000;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;
const double EPS = 0.00000001;
const long long MOD = 1000000007;
const int INF = 1000000010;
const double pi = acos(-1.0);
struct node {
  int x, w;
  node() {}
  node(int x, int w) : x(x), w(w) {}
  bool operator<(const node a) const {
    if (x != a.x) return x < a.x;
    return w < a.w;
  }
} f[N];
int a[2][N], l[2][N], r[2][N], d[2 * N], RI[N];
int main() {
  int i, j, g, n, k = 0, L, R, mid;
  node kk;
  long long ans = 0;
  scanf("%d", &n);
  for (i = 0; i < 2; i++)
    for (j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
      d[++k] = a[i][j];
    }
  sort(d + 1, d + k + 1);
  g = unique(d + 1, d + k + 1) - (d + 1);
  for (i = 0; i < 2; i++)
    for (j = 1; j <= n; j++)
      a[i][j] = lower_bound(d + 1, d + g + 1, a[i][j]) - d;
  a[0][0] = a[0][n + 1] = INF;
  a[1][0] = a[1][n + 1] = 0;
  k = 1;
  d[k] = 0;
  for (i = 1; i <= n; i++) {
    while (a[0][i] >= a[0][d[k]]) k--;
    l[0][i] = d[k];
    d[++k] = i;
  }
  k = 1;
  d[k] = n + 1;
  for (i = n; i >= 1; i--) {
    while (a[0][i] > a[0][d[k]]) k--;
    r[0][i] = d[k];
    d[++k] = i;
  }
  k = 1;
  d[k] = 0;
  for (i = 1; i <= n; i++) {
    while (a[1][i] <= a[1][d[k]]) k--;
    l[1][i] = d[k];
    d[++k] = i;
  }
  k = 1;
  d[k] = n + 1;
  for (i = n; i >= 1; i--) {
    while (a[1][i] < a[1][d[k]]) k--;
    r[1][i] = d[k];
    d[++k] = i;
  }
  k = 1;
  d[k] = n + 1;
  for (i = n; i >= 1; i--) {
    while (a[1][i] <= a[1][d[k]]) k--;
    RI[i] = d[k];
    d[++k] = i;
  }
  for (i = 1; i <= n; i++) f[i] = node(a[1][i], i);
  sort(f + 1, f + n + 1);
  for (i = 1; i <= n; i++) {
    kk = node(a[0][i], i);
    L = 0;
    R = n;
    mid = (L + R) >> 1;
    while (L + 1 < R)
      if (f[mid] < kk) {
        L = mid;
        mid = (L + R) >> 1;
      } else {
        R = mid;
        mid = (L + R) >> 1;
      }
    if (f[L].x == kk.x && r[1][f[L].w] > kk.w && l[1][f[L].w] < kk.w &&
        l[0][i] < f[L].w) {
      if (f[L].w <= kk.w)
        ans += (long long)(f[L].w - max(l[0][i], l[1][f[L].w])) *
               (min(r[0][i], r[1][f[L].w]) - kk.w);
      else
        ans += (long long)(kk.w - max(l[0][i], l[1][f[L].w])) *
               (min(r[0][i], RI[f[L].w]) - f[L].w);
    }
    if (f[R].x == kk.x && l[1][f[R].w] < kk.w && r[1][f[R].w] > kk.w &&
        r[0][i] > f[R].w) {
      if (f[R].w >= kk.w)
        ans += (long long)(kk.w - max(l[0][i], l[1][f[R].w])) *
               (min(r[0][i], RI[f[R].w]) - f[R].w);
      else
        ans += (long long)(f[R].w - max(l[0][i], l[1][f[R].w])) *
               (min(r[0][i], r[1][f[R].w]) - kk.w);
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
