#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, arr1[N], arr2[N], mx[4 * N], mn[4 * N];
long long out;
void bld(int p, int s, int e) {
  if (s == e) {
    mx[p] = arr1[s], mn[p] = arr2[s];
    return;
  }
  int m = (s + e) >> 1, l = p << 1, r = p << 1 | 1;
  bld(l, s, m);
  bld(r, m + 1, e);
  mx[p] = max(mx[l], mx[r]);
  mn[p] = min(mn[l], mn[r]);
}
pair<int, int> qry(int p, int s, int e, int l, int r) {
  if (s > r || e < l) return {INT_MIN, INT_MAX};
  if (s >= l && e <= r) return {mx[p], mn[p]};
  int m = (s + e) >> 1;
  pair<int, int> a = qry(p << 1, s, m, l, r),
                 b = qry(p << 1 | 1, m + 1, e, l, r);
  return {max(a.first, b.first), min(a.second, b.second)};
}
void bs(int idx) {
  int s = idx, e = n - 1, mid, ans1 = -1, ans2 = -1;
  while (s <= e) {
    mid = (s + e) >> 1;
    pair<int, int> t = qry(1, 0, n - 1, idx, mid);
    if (t.first > t.second)
      e = mid - 1;
    else if (t.first < t.second)
      s = mid + 1;
    else
      e = mid - 1, ans1 = mid;
  }
  s = idx, e = n - 1;
  while (s <= e) {
    mid = (s + e) >> 1;
    pair<int, int> t = qry(1, 0, n - 1, idx, mid);
    if (t.first > t.second)
      e = mid - 1;
    else if (t.first < t.second)
      s = mid + 1;
    else
      s = mid + 1, ans2 = mid;
  }
  if (~ans1 && ~ans2) out += ans2 - ans1 + 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> arr1[i];
  for (int i = 0; i < n; ++i) cin >> arr2[i];
  bld(1, 0, n - 1);
  for (int i = 0; i < n; ++i) bs(i);
  cout << out;
  return 0;
}
