#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long p = 1, data = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') p = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    data = data * 10 + (ch ^ 48);
    ch = getchar();
  }
  return p * data;
}
inline long long gcd(long long a, long long b) {
  if (!a || !b) return (!a) ? b : a;
  while (b ^= a ^= b ^= a %= b)
    ;
  return a;
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
void exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b)
    x = 1, y = 0;
  else {
    exgcd(b, a % b, y, x);
    y -= x * (a / b);
  }
}
inline long long qpow(long long a, long long b) {
  long long r = 1;
  while (b) {
    if (b & 1) r = a * r;
    a = a * a;
    b >>= 1;
  }
  return r;
}
const int mod = 1e9 + 7, maxn = 2e5 + 5;
int n, a[maxn], b[maxn];
struct ST_map {
  int st[maxn][30], logg[maxn], bin[maxn];
  void g_st() {
    for (int i = 1; i <= n; i++) st[i][0] = read();
    bin[0] = 1;
    for (int i = 1; i <= 30; i++) bin[i] = bin[i - 1] * 2;
    logg[0] = -1;
    for (int i = 1; i <= n; i++) logg[i] = logg[i / 2] + 1;
    for (int i = 1; i <= logg[n]; i++)
      for (int j = 1; j <= n; j++)
        if (j + bin[i] - 1 <= n)
          st[j][i] = max(st[j][i - 1], st[j + bin[i - 1]][i - 1]);
        else
          break;
  }
  int query(int l, int r) {
    int len = r - l + 1, lg = logg[len];
    return max(st[l][lg], st[r - bin[lg] + 1][lg]);
  }
} st;
struct ST__map {
  int st[maxn][30], logg[maxn], bin[maxn];
  void g_st() {
    for (int i = 1; i <= n; i++) st[i][0] = read();
    bin[0] = 1;
    for (int i = 1; i <= 30; i++) bin[i] = bin[i - 1] * 2;
    logg[0] = -1;
    for (int i = 1; i <= n; i++) logg[i] = logg[i / 2] + 1;
    for (int i = 1; i <= logg[n]; i++)
      for (int j = 1; j <= n; j++)
        if (j + bin[i] - 1 <= n)
          st[j][i] = min(st[j][i - 1], st[j + bin[i - 1]][i - 1]);
        else
          break;
  }
  int query(int l, int r) {
    int len = r - l + 1, lg = logg[len];
    return min(st[l][lg], st[r - bin[lg] + 1][lg]);
  }
} stt;
int main() {
  n = read();
  long long ans = 0;
  st.g_st();
  stt.g_st();
  for (int i = 1; i <= n; i++) {
    int L = 0x3f3f3f3f, R = -0x3f3f3f3f;
    int l = i, r = n, mid;
    while (l <= r) {
      mid = l + r >> 1;
      if (st.query(i, mid) > stt.query(i, mid))
        r = mid - 1;
      else if (st.query(i, mid) < stt.query(i, mid))
        l = mid + 1;
      else {
        L = min(L, mid);
        r = mid - 1;
      }
    }
    l = i, r = n;
    while (l <= r) {
      mid = l + r >> 1;
      if (st.query(i, mid) > stt.query(i, mid))
        r = mid - 1;
      else if (st.query(i, mid) < stt.query(i, mid))
        l = mid + 1;
      else {
        R = max(R, mid);
        l = mid + 1;
      }
    }
    if (L != 0x3f3f3f3f && R != -0x3f3f3f3f) ans += R - L + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
