#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 1;
long long res, n, a[maxN], b[maxN], i, t, fa[20][maxN], fb[20][maxN], j, l, h,
    k1, k2;
int Log(int x) {
  int cnt = 0;
  while (x != 1) x = x / 2, cnt++;
  return cnt;
}
void Init() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i], fa[0][i] = a[i];
  for (i = 1; i <= n; i++) cin >> b[i], fb[0][i] = b[i];
  t = Log(n);
}
void Rmq() {
  for (i = 1; i <= t; i++) {
    for (j = 1; j <= n; j++) {
      int dis = (1 << i - 1);
      int l = j + dis;
      if (l <= n)
        fa[i][j] = max(fa[i - 1][j], fa[i - 1][l]);
      else
        fa[i][j] = fa[i - 1][j];
    }
  }
  for (i = 1; i <= t; i++) {
    for (j = 1; j <= n; j++) {
      int dis = (1 << i - 1);
      int l = j + dis;
      if (l <= n)
        fb[i][j] = min(fb[i - 1][j], fb[i - 1][l]);
      else
        fb[i][j] = fb[i - 1][j];
    }
  }
}
int Check(int rang1, int rang2) {
  int lg = Log(rang2 - rang1 + 1);
  rang2 = rang2 - (1 << lg) + 1;
  int u = max(fa[lg][rang1], fa[lg][rang2]);
  int v = min(fb[lg][rang1], fb[lg][rang2]);
  if (u < v) return 1;
  if (u == v) return 2;
  if (u > v) return 3;
}
void Solve() {
  for (i = 1; i <= n; i++) {
    l = i;
    h = n;
    k1 = n + 1;
    while (l <= h) {
      int m = (l + h) / 2;
      if (Check(i, m) == 1)
        l = m + 1;
      else if (Check(i, m) == 2)
        k1 = min(k1, (long long)m), h = m - 1;
      else if (Check(i, m) == 3)
        h = m - 1;
    }
    if (k1 == n + 1) continue;
    l = k1;
    h = n;
    while (l <= h) {
      int m = (l + h) / 2;
      if (Check(i, m) == 2)
        l = m + 1, k2 = m;
      else
        h = m - 1;
    }
    res += k2 - k1 + 1;
  }
  cout << res;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  Init();
  Rmq();
  Solve();
}
