#include <bits/stdc++.h>
using namespace std;
int n;
int sa[21][300000];
int sb[21][300000];
int a[300000];
int b[300000];
long long ans;
int lg[500000];
int sz;
map<int, int> mp;
int geta(int l, int r) {
  int sz = r - l + 1;
  int h = lg[sz];
  return max(sa[h][l], sa[h][r - (1 << (h)) + 1]);
}
int getb(int l, int r) {
  int sz = r - l + 1;
  int h = lg[sz];
  return min(sb[h][l], sb[h][r - (1 << (h)) + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  lg[1] = 0;
  for (int i = 2; i <= 300000; i++) lg[i] = 1 + lg[i / 2];
  for (sz = 1; sz < n; sz += sz)
    ;
  for (int i = n + 1; i <= sz; i++) {
    b[i] = 1e9 + 9;
    a[i] = -b[i];
  }
  for (int i = 1; i <= sz; i++) {
    sa[0][i] = a[i];
    sb[0][i] = b[i];
  }
  for (int h = 1; (1 << h) <= n; h++) {
    for (int i = 1; i + (1 << h) - 1 <= n; i++) {
      sa[h][i] = max(sa[h - 1][i], sa[h - 1][i + (1 << (h - 1))]);
      sb[h][i] = min(sb[h - 1][i], sb[h - 1][i + (1 << (h - 1))]);
    }
  }
  for (int i = 1; i <= n; i++) {
    int l, r, ll, rr;
    l = i;
    r = n;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (geta(i, mid) <= getb(i, mid))
        l = mid;
      else
        r = mid;
    }
    while (r >= i && geta(i, r) > getb(i, r)) --r;
    rr = r;
    l = i;
    r = n;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (geta(i, mid) >= getb(i, mid))
        r = mid;
      else
        l = mid;
    }
    while (l <= n && geta(i, l) < getb(i, l)) ++l;
    ll = l;
    ans += max(0, rr - ll + 1);
  }
  cout << ans << "\n";
}
