#include <bits/stdc++.h>
using namespace std;
int n, rmq[2][505050][19];
inline int op(int a, int b, int c) { return c ? min(a, b) : max(a, b); }
inline int RMQ(int l, int r, int tp) {
  int k = __builtin_clz(1) - __builtin_clz(r - l + 1);
  return op(rmq[tp][l][k], rmq[tp][r - (1 << k) + 1][k], tp);
}
int main() {
  scanf("%d", &n);
  for (int(tp) = (0); (tp) < (2); (tp)++) {
    for (int(i) = (0); (i) < (n); (i)++) scanf("%d", &rmq[tp][i][0]);
    for (int(j) = (1); (j) < (19); (j)++)
      for (int(i) = (0); (i) < (n - (1 << j) + 1); (i)++)
        rmq[tp][i][j] =
            op(rmq[tp][i][j - 1], rmq[tp][i + (1 << (j - 1))][j - 1], tp);
  }
  long long ans = 0;
  for (int(i) = (0); (i) < (n); (i)++) {
    int ini = i, fim = n - 1, mid, best = i - 1;
    while (ini <= fim) {
      mid = (ini + fim) >> 1;
      if (RMQ(i, mid, 0) < RMQ(i, mid, 1))
        best = mid, ini = mid + 1;
      else
        fim = mid - 1;
    }
    ans -= best + 1;
    ini = i, fim = n - 1, best = n;
    while (ini <= fim) {
      mid = (ini + fim) >> 1;
      if (RMQ(i, mid, 0) > RMQ(i, mid, 1))
        best = mid, fim = mid - 1;
      else
        ini = mid + 1;
    }
    ans += best;
  }
  printf("%lld\n", ans);
}
