#include <bits/stdc++.h>
using namespace std;
vector<int> a, b;
int dp[2][20][200005];
void make() {
  int n = int((a).size());
  for (int i = 0; i < int(n); i++) {
    dp[0][0][i] = a[i];
    dp[1][0][i] = b[i];
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) - 1 < n; i++) {
      dp[0][j][i] = max(dp[0][j - 1][i], dp[0][j - 1][i + (1 << (j - 1))]);
      dp[1][j][i] = min(dp[1][j - 1][i], dp[1][j - 1][i + (1 << (j - 1))]);
    }
  }
}
int read(int i, int j, int f) {
  int l = 31 - __builtin_clz(j - i + 1);
  if (f) {
    return min(dp[f][l][i], dp[f][l][j - (1 << l) + 1]);
  } else {
    return max(dp[f][l][i], dp[f][l][j - (1 << l) + 1]);
  }
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (int i = 0; i < int(n); i++) {
    cin >> a[i];
  }
  for (int i = 0; i < int(n); i++) {
    cin >> b[i];
  }
  make();
  long long ans = 0;
  for (int i = 0; i < int(n); i++) {
    int low = i, high = n - 1;
    while (low <= high) {
      int mid = (high + low) / 2;
      int mn = read(i, mid, 1), mx = read(i, mid, 0);
      if (mn < mx) {
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    int t = high;
    low = i, high = n - 1;
    while (low <= high) {
      int mid = (high + low) / 2;
      int mn = read(i, mid, 1), mx = read(i, mid, 0);
      if (mn > mx) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    int s = low;
    ans += t - s + 1;
  }
  cout << ans;
  return 0;
}
