#include <bits/stdc++.h>
using namespace std;
ifstream fi("d.inp");
ofstream fo("d.out");
const int maxn = 2e5 + 5;
const int lim = 1e18 + 5;
int n;
long long a[maxn], b[maxn];
long long f[maxn][22], g[maxn][22];
long long res = 0;
void enter() {
  cin >> n;
  for (int i = (1); i <= (n); ++i) cin >> a[i];
  for (int i = (1); i <= (n); ++i) cin >> b[i];
}
void init1() {
  for (int i = (1); i <= (n); ++i) f[i][0] = a[i];
  int t = log2(n);
  for (int i = (1); i <= (t); ++i)
    for (int u = (1); u <= (n - (1 << i) + 1); ++u)
      f[u][i] = max(f[u][i - 1], f[u + (1 << (i - 1))][i - 1]);
}
void init2() {
  for (int i = (1); i <= (n); ++i) g[i][0] = b[i];
  int t = log2(n);
  for (int i = (1); i <= (t); ++i)
    for (int u = (1); u <= (n - (1 << i) + 1); ++u)
      g[u][i] = min(g[u][i - 1], g[u + (1 << (i - 1))][i - 1]);
}
int get1(int l, int r) {
  int t = log2(r - l + 1);
  int res = max(f[l][t], f[r - (1 << t) + 1][t]);
  return res;
}
int get2(int l, int r) {
  int t = log2(r - l + 1);
  int res = min(g[l][t], g[r - (1 << t) + 1][t]);
  return res;
}
int find_1(int l) {
  int d = l, c = n;
  int ans = n + 1;
  while (d <= c) {
    int g = (d + c) >> 1;
    long long u = get1(l, g), v = get2(l, g);
    if (u >= v) {
      ans = min(ans, g);
      c = g - 1;
    } else
      d = g + 1;
  }
  return ans;
}
int find_2(int l) {
  int d = l, c = n;
  int ans = l - 1;
  while (d <= c) {
    int g = (d + c) >> 1;
    long long u = get1(l, g), v = get2(l, g);
    if (u <= v) {
      ans = max(ans, g);
      d = g + 1;
    } else
      c = g - 1;
  }
  return ans;
}
void solve() {
  init1();
  init2();
  for (int i = (1); i <= (n); ++i) {
    int st = find_1(i), fn = find_2(i);
    if (st != n + 1 && fn != i - 1 && st <= fn) res += fn - st + 1;
  }
  cout << res;
}
int main() {
  enter();
  solve();
}
