#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], b[N], maxi[N][25], mini[N][25];
void RMQ() {
  for (int i = 1; i <= n; i++) {
    maxi[i][0] = a[i];
    mini[i][0] = b[i];
  }
  for (int i = 1; (1 << i) <= n; i++) {
    for (int j = 1; j + (1 << i) - 1 <= n; j++) {
      maxi[j][i] = max(maxi[j][i - 1], maxi[j + (1 << (i - 1))][i - 1]);
      mini[j][i] = min(mini[j][i - 1], mini[j + (1 << (i - 1))][i - 1]);
    }
  }
}
int ask_max(int l, int r) {
  int tmp = log(r - l + 1) / log(2);
  return max(maxi[l][tmp], maxi[r - (1 << tmp) + 1][tmp]);
}
int ask_min(int l, int r) {
  int tmp = log(r - l + 1) / log(2);
  return min(mini[l][tmp], mini[r - (1 << tmp) + 1][tmp]);
}
int lower_bd(int l) {
  int ll = l, rr = n;
  while (ll + 1 != rr) {
    int mid = (ll + rr) / 2;
    if (ask_max(l, mid) >= ask_min(l, mid))
      rr = mid;
    else
      ll = mid;
  }
  if (ask_max(l, ll) == ask_min(l, ll))
    return ll;
  else if (ask_max(l, rr) == ask_min(l, rr))
    return rr;
  else
    return -1;
}
int upper_bd(int l) {
  int ll = l, rr = n;
  while (ll + 1 != rr) {
    int mid = (ll + rr) / 2;
    if (ask_max(l, mid) > ask_min(l, mid))
      rr = mid;
    else
      ll = mid;
  }
  if (ask_max(l, rr) == ask_min(l, rr))
    return rr;
  else if (ask_max(l, ll) == ask_min(l, ll))
    return ll;
  else
    return -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]);
  RMQ();
  long long ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    int u = lower_bd(i), v = upper_bd(i);
    if (u != -1 && v != -1) ans += v - u + 1;
  }
  if (a[n] == b[n]) ans++;
  printf("%I64d\n", ans);
  return 0;
}
