#include <bits/stdc++.h>
using namespace std;
const int inf = 1e09 + 31;
const int mxn = 200005;
const int k = 19;
int logv[mxn + 9];
int sta[mxn][k + 1], stb[mxn][k + 1];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> a, b;
  int temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a.push_back(temp);
  }
  a.push_back(inf);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    b.push_back(temp);
  }
  b.push_back(0);
  logv[1] = 0;
  for (int i = 2; i <= mxn + 8; i++) logv[i] = logv[i / 2] + 1;
  for (int i = 0; i <= n; i++) sta[i][0] = a[i];
  for (int j = 1; j <= k; j++)
    for (int i = 0; i + (1 << j) <= n; i++)
      sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
  for (int i = 0; i <= n; i++) stb[i][0] = b[i];
  for (int j = 1; j <= k; j++)
    for (int i = 0; i + (1 << j) <= n; i++)
      stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int sl = i, en = n;
    while (sl < en) {
      int mid = (sl + en + 0) / 2;
      if (max(sta[i][logv[mid - i + 1]],
              sta[mid - (1 << logv[mid - i + 1]) + 1][logv[mid - i + 1]]) >=
          min(stb[i][logv[mid - i + 1]],
              stb[mid - (1 << logv[mid - i + 1]) + 1][logv[mid - i + 1]])) {
        en = mid;
      } else {
        sl = mid + 1;
      }
    }
    int su = i, eu = n;
    while (su < eu) {
      int mid = (su + eu + 0) / 2;
      if (max(sta[i][logv[mid - i + 1]],
              sta[mid - (1 << logv[mid - i + 1]) + 1][logv[mid - i + 1]]) >
          min(stb[i][logv[mid - i + 1]],
              stb[mid - (1 << logv[mid - i + 1]) + 1][logv[mid - i + 1]])) {
        eu = mid;
      } else {
        su = mid + 1;
      }
    }
    ans += su - sl;
  }
  cout << ans;
}
