#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int dpmin[maxn][25];
int dpmax[maxn][25];
int mn[maxn];
int n, k;
int a[maxn], b[maxn];
void Init() {
  for (int i = (1); i <= (n); i++) dpmin[i][0] = b[i], dpmax[i][0] = a[i];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      dpmin[i][j] = min(dpmin[i][j - 1], dpmin[i + (1 << j - 1)][j - 1]),
      dpmax[i][j] = max(dpmax[i][j - 1], dpmax[i + (1 << j - 1)][j - 1]);
}
int check(int l, int r) {
  int x = log2((r - l + 1));
  int MAX = max(dpmax[l][x], dpmax[r - (1 << x) + 1][x]);
  int MIN = min(dpmin[l][x], dpmin[r - (1 << x) + 1][x]);
  return MAX - MIN;
}
long long solve() {
  long long ans = 0;
  for (int i = (1); i <= (n); i++) {
    int l = i, r = n, q = -1;
    while (l <= r) {
      int mid = l + r >> 1;
      if (check(i, mid) == 0) q = mid;
      if (check(i, mid) <= 0)
        l = mid + 1;
      else
        r = mid - 1;
    }
    if (q == -1) continue;
    l = i, r = n;
    int p = -1;
    while (l <= r) {
      int mid = l + r >> 1;
      if (check(i, mid) >= 0)
        p = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    ans += q - p + 1;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
  for (int i = (1); i <= (n); i++) scanf("%d", &b[i]);
  Init();
  cout << solve() << endl;
  return 0;
}
