#include <bits/stdc++.h>
using namespace std;
int a[200010], b[200010];
int n;
int ma[200010][20], mi[200010][20];
void cal() {
  for (int i = 0; i < n; i++) {
    ma[i][0] = a[i];
    mi[i][0] = b[i];
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      ma[i][j] = max(ma[i][j - 1], ma[i + (1 << (j - 1))][j - 1]);
      mi[i][j] = min(mi[i][j - 1], mi[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int RMQmax(int le, int ri) {
  int k = 0;
  while ((1 << (k + 1)) <= ri - le + 1) k++;
  return max(ma[le][k], ma[ri - (1 << k) + 1][k]);
}
int RMQmin(int le, int ri) {
  int k = 0;
  while ((1 << (k + 1)) <= ri - le + 1) k++;
  return min(mi[le][k], mi[ri - (1 << k) + 1][k]);
}
int binsearch_min(int lef, int rig) {
  int ans = -1;
  int low = lef, high = rig;
  while (low <= high) {
    int mid = (low + high) / 2;
    long long key = RMQmax(lef, mid) - RMQmin(lef, mid);
    if (key == 0) {
      ans = mid;
      high = mid - 1;
    } else if (key > 0) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return ans;
}
int binsearch_max(int lef, int rig) {
  int ans = -1;
  int low = lef, high = rig;
  while (low <= high) {
    int mid = (low + high) / 2;
    long long key = RMQmax(lef, mid) - RMQmin(lef, mid);
    if (key == 0) {
      ans = mid;
      low = mid + 1;
    } else if (key > 0) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return ans;
}
int main() {
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  cal();
  for (int i = 0; i < n; i++) {
    int low = binsearch_min(i, n - 1);
    int high = binsearch_max(i, n - 1);
    if (low != -1) {
      ans += (high - low) + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
