#include <bits/stdc++.h>
const int MAX_N = (int)2e5 + 123;
const double eps = 1e-6;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9 + 123;
using namespace std;
int n;
int a[MAX_N], b[MAX_N];
int maxi[20][MAX_N], mini[20][MAX_N], lg[MAX_N];
void calc_max() {
  for (int i = 1; i <= n; i++) maxi[0][i] = a[i];
  for (int it = 1; it < 20; it++) {
    for (int i = 1; i + (1 << it) - 1 <= n; i++) {
      maxi[it][i] = max(maxi[it - 1][i], maxi[it - 1][i + (1 << (it - 1))]);
    }
  }
}
void calc_min() {
  for (int i = 1; i <= n; i++) mini[0][i] = b[i];
  for (int it = 1; it < 20; it++) {
    for (int i = 1; i + (1 << it) - 1 <= n; i++) {
      mini[it][i] = min(mini[it - 1][i], mini[it - 1][i + (1 << (it - 1))]);
    }
  }
}
int get_max(int l, int r) {
  int g = lg[r - l + 1];
  return max(maxi[g][l], maxi[g][r - (1 << g) + 1]);
}
int get_min(int l, int r) {
  int g = lg[r - l + 1];
  return min(mini[g][l], mini[g][r - (1 << g) + 1]);
}
int get_cnt_max(int id, int R) {
  int l = id, r = R, mid = -1, ans = -1;
  int val = get_max(id, R);
  while (l <= r) {
    mid = (l + r) / 2;
    if (get_max(id, mid) == val) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  assert(ans != -1);
  return ans;
}
int get_cnt_min(int id, int R) {
  int l = id, r = R, mid = -1, ans = -1;
  int val = get_min(id, R);
  while (l <= r) {
    mid = (l + r) / 2;
    if (get_min(id, mid) == val) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  assert(ans != -1);
  return ans;
}
long long int all;
void calc(int id) {
  int l = id, r = n, mid = -1, ans = n + 1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (get_max(id, mid) > get_min(id, mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  ans--;
  if (ans < id || get_max(id, ans) != get_min(id, ans)) return;
  all += (ans - max(get_cnt_max(id, ans), get_cnt_min(id, ans)) + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  calc_max(), calc_min();
  for (int i = 1, j = 0; i <= n; i++) {
    if ((1 << (j + 1)) == i) j++;
    lg[i] = j;
  }
  for (int i = 1; i <= n; i++) calc(i);
  printf("%lld", all);
  return 0;
}
