#include <bits/stdc++.h>
using namespace std;
int dx[20][1 << 20];
int dy[20][1 << 20];
int lg[1 << 20];
int mx(int l, int r) {
  int p = lg[r - l + 1];
  return max(dx[p][l], dx[p][r - (1 << p) + 1]);
}
int mn(int l, int r) {
  int p = lg[r - l + 1];
  return min(dy[p][l], dy[p][r - (1 << p) + 1]);
}
int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &dx[0][i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &dy[0][i]);
  for (int p = 1; (1 << p) <= n; ++p)
    for (int i = 1; i + (1 << p) - 1 <= n; ++i)
      dx[p][i] = max(dx[p - 1][i], dx[p - 1][i + (1 << (p - 1))]),
      dy[p][i] = min(dy[p - 1][i], dy[p - 1][i + (1 << (p - 1))]);
  for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
  long long ans = 0;
  for (int i = 1; i <= n; ++i)
    if (mx(i, i) <= mn(i, i)) {
      int l = i - 1, r = i;
      for (int p = 1 << lg[n - i + 1]; p; p >>= 1) {
        if (p + l <= n && mx(i, l + p) < mn(i, l + p)) l += p;
        if (p + r <= n && mx(i, r + p) <= mn(i, r + p)) r += p;
      }
      if (dx[0][0] == 198654465 && r > l)
        cout << i << ' ' << l << ' ' << r << '\n';
      ans += r - l;
    }
  cout << ans << '\n';
}
