#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 5;
int minst[N][27];
int maxst[N][27];
int n;
void inst() {
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      minst[i][j] = min(minst[i][j - 1], minst[i + (1 << (j - 1))][j - 1]);
      maxst[i][j] = max(maxst[i][j - 1], maxst[i + (1 << (j - 1))][j - 1]);
    }
  }
  return;
}
int query(int l, int r) {
  int x = log(r - l + 1.0) / log(2.0);
  int maxx = max(maxst[l][x], maxst[r - (1 << x) + 1][x]);
  int minn = min(minst[l][x], minst[r - (1 << x) + 1][x]);
  if (maxx > minn) return 1;
  if (maxx == minn) return 0;
  return -1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &maxst[i][0]);
  for (int i = 1; i <= n; i++) scanf("%d", &minst[i][0]);
  inst();
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int l = i, r = n;
    int fr = 0, la = 0;
    while (l <= r) {
      int m = (l + r) >> 1;
      if (query(i, m) == 0) fr = m;
      if (query(i, m) >= 0)
        r = m - 1;
      else
        l = m + 1;
    }
    if (fr == 0) continue;
    l = fr, r = n;
    while (l <= r) {
      int m = (l + r) >> 1;
      if (query(i, m) > 0)
        r = m - 1;
      else
        la = m, l = m + 1;
    }
    ans += 1ll * (la - fr + 1);
  }
  cout << ans << endl;
  return 0;
}
