#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int a[N], b[N];
int dp1[N][22], dp2[N][22];
int n;
int getmax(int l, int r) {
  int k = log2(r - l + 1);
  return max(dp1[l][k], dp1[r - (1 << k) + 1][k]);
}
int getmin(int l, int r) {
  int k = log2(r - l + 1);
  return min(dp2[l][k], dp2[r - (1 << k) + 1][k]);
}
int low(int pos) {
  int ans = -1;
  int l = pos, r = n, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (getmax(pos, mid) >= getmin(pos, mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  return r;
}
int upp(int pos) {
  int ans = -1;
  int l = pos, r = n, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (getmax(pos, mid) > getmin(pos, mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  return r;
}
int main() {
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), dp1[i][0] = a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), dp2[i][0] = b[i];
  for (int i = 1; i < 22; i++)
    for (int j = 1; j + (1 << i) - 1 <= n; j++)
      dp1[j][i] = max(dp1[j][i - 1], dp1[j + (1 << (i - 1))][i - 1]);
  for (int i = 1; i < 22; i++)
    for (int j = 1; j + (1 << i) - 1 <= n; j++)
      dp2[j][i] = min(dp2[j][i - 1], dp2[j + (1 << (i - 1))][i - 1]);
  for (int i = 1; i <= n; i++) {
    ans += upp(i) - low(i);
  }
  cout << ans << endl;
  return 0;
}
