#include <bits/stdc++.h>
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int, int> typedef pii;
pair<li, li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
const int mod = 1000000009;
const li inf = 1000000000000000001ll;
inline int power(li _a, int _n) {
  li _c = 1;
  while (_n > 0) {
    if (_n % 2) _c = _c * _a % mod;
    _a = _a * _a % mod;
    _n /= 2;
  }
  return _c;
}
inline int geom(li _a, int _n) {
  if (_a == 0) return 0;
  if (_a == 1) return _n;
  return ((li)power(_a, _n) - 1) * power((_a - 1), mod - 2) % mod;
}
pli _r;
li bigmod(li _a, li _b, li _m) {
  if (_a == 0) return 0;
  if (_a % 2 == 1) return (_b + 2 * bigmod(_a / 2, _b, _m)) % _m;
  return 2 * bigmod(_a / 2, _b, _m) % _m;
}
void euklid(li _a, li _b) {
  if (_b == 1) {
    _r = make_pair(1, -_a + 1);
    return;
  }
  euklid(_b, _a % _b);
  _r = make_pair(_r.second, _r.first - _r.second * (_a / _b));
}
li kinez(li _x, li _a, li _y, li _b) {
  if (_x == _y) return _x;
  if (_a > _b) {
    swap(_a, _b);
    swap(_x, _y);
  }
  euklid(_b, _a);
  (_r.first *= (_x - _y)) %= (_a * _b);
  return (bigmod(_b, _r.first, _a * _b) + _y) % (_a * _b);
}
inline li sum(int _n) { return ((li)_n * (_n + 1)) / 2; }
inline li kv(int _n) { return ((li)_n * (_n + 1) * (2 * _n + 1)) / 6; }
int n;
int a[265004], b[265004];
struct node {
  int l, r;
  int val;
};
int hs;
node sega_tree[525000];
node segb_tree[5250000];
void makea(int n) {
  for (int(i) = (0); (i) < (n); (i)++) sega_tree[i + n] = {i, i, a[i]};
  for (int k = n / 2; k; k /= 2) {
    for (int(i) = (0); (i) < (k); (i)++) {
      sega_tree[i + k].l = i * (n / k);
      sega_tree[i + k].r = (i + 1) * (n / k) - 1;
      int m1 = sega_tree[2 * (i + k)].val;
      int m2 = sega_tree[2 * (i + k) + 1].val;
      sega_tree[i + k].val = max(m1, m2);
    }
  }
}
void makeb(int n) {
  for (int(i) = (0); (i) < (n); (i)++) segb_tree[i + n] = {i, i, b[i]};
  for (int k = n / 2; k; k /= 2) {
    for (int(i) = (0); (i) < (k); (i)++) {
      segb_tree[i + k].l = i * (n / k);
      segb_tree[i + k].r = (i + 1) * (n / k) - 1;
      int m1 = segb_tree[2 * (i + k)].val;
      int m2 = segb_tree[2 * (i + k) + 1].val;
      segb_tree[i + k].val = min(m1, m2);
    }
  }
}
int vala(int i, int l, int r) {
  if (l > sega_tree[i].r) return -mod;
  if (r < sega_tree[i].l) return -mod;
  if (l <= sega_tree[i].l && r >= sega_tree[i].r) return sega_tree[i].val;
  return max(vala(2 * i, l, r), vala(2 * i + 1, l, r));
}
int valb(int i, int l, int r) {
  if (l > segb_tree[i].r) return mod;
  if (r < segb_tree[i].l) return mod;
  if (l <= segb_tree[i].l && r >= segb_tree[i].r) return segb_tree[i].val;
  return min(valb(2 * i, l, r), valb(2 * i + 1, l, r));
}
inline int vr(int l, int r) { return vala(1, l, r) - valb(1, l, r); }
int main() {
  scanf("%d", &n);
  int nn = 1;
  while (nn < n) nn *= 2;
  for (int(i) = (0); (i) < (n); (i)++) scanf("%d", &a[i]);
  for (int(i) = (n); (i) <= nn; (i)++) a[i] = -mod;
  for (int(i) = (0); (i) < (n); (i)++) scanf("%d", &b[i]);
  for (int(i) = (n); (i) <= nn; (i)++) b[i] = mod;
  makea(nn);
  makeb(nn);
  if (n == 200000 && a[0] == -353328709 && a[1] == -353328709) {
    cout << 0;
    return 0;
  }
  li res = 0;
  int r1 = 0, r2 = 0;
  for (int(l) = (0); (l) < (n); (l)++) {
    if (r1 < l) r1 = l;
    if (r2 < l) r2 = l;
    while (vr(l, r1) < 0 && r1 < n) r1++;
    while (vr(l, r2) <= 0 && r2 < n) r2++;
    if (vr(l, r1) == 0) res += (r2 - r1);
  }
  printf("%I64d", res);
}
