#include <bits/stdc++.h>
using namespace std;
int Max(int a, int b) { return a > b ? a : b; }
int Min(int a, int b) { return a < b ? a : b; }
int a[200010], b[200010];
int aa[200010][25], bb[200010][25];
int n;
void rmq_inita() {
  for (int i = 0; i < n; i++) aa[i][0] = a[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      aa[i][j] = max(aa[i][j - 1], aa[i + (1 << (j - 1))][j - 1]);
    }
  }
}
void rmq_initb() {
  for (int i = 0; i < n; i++) bb[i][0] = b[i];
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      bb[i][j] = min(bb[i][j - 1], bb[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int rmqa(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return max(aa[L][k], aa[R - (1 << k) + 1][k]);
}
int rmqb(int L, int R) {
  int k = 0;
  while ((1 << (k + 1)) <= R - L + 1) k++;
  return min(bb[L][k], bb[R - (1 << k) + 1][k]);
}
int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < n; i++) scanf("%d", &b[i]);
    rmq_inita();
    rmq_initb();
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int l = i, r = n - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (rmqa(i, mid) - rmqb(i, mid) < 0)
          l = mid + 1;
        else
          r = mid - 1;
      }
      ans -= l;
      l = i, r = n - 1;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (rmqa(i, mid) - rmqb(i, mid) <= 0)
          l = mid + 1;
        else
          r = mid - 1;
      }
      ans += l;
    }
    cout << ans << endl;
  }
  return 0;
}
