#include <bits/stdc++.h>
using namespace std;
int n, a[200010], b[200010], sta[200010][32], stb[200010][32];
long long ans;
int finda(int l, int r) {
  int k = log2(r - l + 1);
  return max(sta[l][k], sta[r - (1 << k) + 1][k]);
}
int findb(int l, int r) {
  int k = log2(r - l + 1);
  return min(stb[l][k], stb[r - (1 << k) + 1][k]);
}
int findl(int s) {
  int tmp = s - 1, l = s, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (finda(s, mid) < findb(s, mid))
      tmp = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return tmp;
}
int findr(int s) {
  int tmp = 0, l = s, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (findb(s, mid) >= finda(s, mid))
      tmp = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return tmp;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sta[i][0] = a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), stb[i][0] = b[i];
  for (int j = 1; j <= log2(n); j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      sta[i][j] = max(sta[i][j - 1], sta[i + (1 << j - 1)][j - 1]),
      stb[i][j] = min(stb[i][j - 1], stb[i + (1 << j - 1)][j - 1]);
  for (int i = 1; i <= n; i++) {
    int r = findr(i), l = findl(i);
    if (r < l) continue;
    ans += r - l;
  }
  cout << ans;
  return 0;
}
