#include <bits/stdc++.h>
using namespace std;
long long n, a[200005], b[200005], rmqa[200005][20], rmqb[200005][20];
long long MAX(int a, int b) { return a > b ? a : b; }
long long MIN(int a, int b) { return a < b ? a : b; }
long long querya(int l, int r) {
  int k = log(r - l + 1) / log(2);
  return MAX(rmqa[l][k], rmqa[r - (1 << k) + 1][k]);
}
long long queryb(int l, int r) {
  int k = log(r - l + 1) / log(2);
  return MIN(rmqb[l][k], rmqb[r - (1 << k) + 1][k]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], rmqa[i][0] = a[i];
  for (int i = 1; i <= n; i++) cin >> b[i], rmqb[i][0] = b[i];
  for (int k = 1; k <= 20; k++) {
    for (int i = 1; i <= n; i++) {
      if (i + (1 << k) - 1 <= n) {
        rmqa[i][k] = MAX(rmqa[i][k - 1], rmqa[i + (1 << (k - 1))][k - 1]);
        rmqb[i][k] = MIN(rmqb[i][k - 1], rmqb[i + (1 << (k - 1))][k - 1]);
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > b[i]) continue;
    int l = i, r = n, mid, s1, s2;
    while (r >= l) {
      mid = (l + r) >> 1;
      if (querya(i, mid) >= queryb(i, mid)) {
        r = mid - 1;
      } else
        l = mid + 1;
    }
    s1 = l;
    l = i, r = n;
    while (r >= l) {
      mid = (l + r) >> 1;
      if (querya(i, mid) > queryb(i, mid)) {
        r = mid - 1;
      } else
        l = mid + 1;
    }
    s2 = r;
    ans += s2 - s1 + 1;
  }
  cout << ans << endl;
}
