#include <bits/stdc++.h>
using namespace std;
int n, a[200005], b[200005];
void enter() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
}
int f[200005][20], g[200005][20];
void rmq() {
  for (int i = 1; i <= n; i++) f[i][0] = a[i], g[i][0] = b[i];
  int l = log2(n);
  for (int j = 1; j <= l; j++)
    for (int i = 1; i <= n; i++)
      if (i + (1 << (j - 1)) <= n)
        f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
  for (int j = 1; j <= l; j++)
    for (int i = 1; i <= n; i++)
      if (i + (1 << (j - 1)) <= n)
        g[i][j] = min(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
}
int get_max_a(int u, int v) {
  int l = log2(v - u + 1);
  return max(f[u][l], f[v - (1 << l) + 1][l]);
}
int get_min_b(int u, int v) {
  int l = log2(v - u + 1);
  return min(g[u][l], g[v - (1 << l) + 1][l]);
}
int find_(int cl) {
  int l = cl, r = n, res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (get_max_a(cl, mid) == get_min_b(cl, mid)) {
      r = mid - 1;
      res = mid;
    } else if (get_max_a(cl, mid) < get_min_b(cl, mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}
int find__(int cl) {
  int l = cl, r = n, res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (get_max_a(cl, mid) == get_min_b(cl, mid)) {
      l = mid + 1;
      res = mid;
    } else if (get_max_a(cl, mid) < get_min_b(cl, mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}
void process() {
  rmq();
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    int t1 = find_(i), t2 = find__(i);
    if (t1 == -1) continue;
    res += t2 - t1 + 1;
  }
  cout << res << endl;
}
int main() {
  enter();
  process();
  return 0;
}
