#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 7;
long long int a[MAXN], b[MAXN], maxsum[MAXN][30], minsum[MAXN][30];
void RMQ(int num) {
  for (int j = 1; (1 << j) <= num; ++j)
    for (int i = 1; i <= num; ++i)
      if (i + (1 << j) - 1 <= num) {
        maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
        minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
      }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), maxsum[i][0] = a[i];
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]), minsum[i][0] = b[i];
  RMQ(n);
  long long int sum = 0;
  for (int i = 1; i <= n; i++) {
    int l = i, r = n;
    long long int ans1 = -1, ans2 = -1;
    while (r >= l) {
      int mid = (l + r) / 2;
      int k = (int)(log(mid - i + 1.0) / log(2.0));
      long long int maxres = max(maxsum[i][k], maxsum[mid - (1 << k) + 1][k]);
      long long int minres = min(minsum[i][k], minsum[mid - (1 << k) + 1][k]);
      if (maxres >= minres)
        r = mid - 1, ans1 = mid;
      else
        l = mid + 1;
    }
    l = i, r = n;
    while (r >= l) {
      int mid = (l + r) / 2;
      int k = (int)(log(mid - i + 1.0) / log(2.0));
      long long int maxres = max(maxsum[i][k], maxsum[mid - (1 << k) + 1][k]);
      long long int minres = min(minsum[i][k], minsum[mid - (1 << k) + 1][k]);
      if (maxres > minres)
        r = mid - 1;
      else
        l = mid + 1, ans2 = mid;
    }
    if (ans1 == -1 || ans2 == -1) continue;
    sum += ans2 - ans1 + 1;
  }
  printf("%lld\n", sum);
}
