#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int inf = 0x3f3f3f3f;
int n;
int a[maxn][20], b[maxn][20];
int queryMax(int l, int r) {
  int k = log((r - l + 1) * 1.0) / log(2.0);
  return max(a[l][k], a[r + 1 - (1 << k)][k]);
}
int queryMin(int l, int r) {
  int k = log((r - l + 1) * 1.0) / log(2.0);
  return min(b[l][k], b[r + 1 - (1 << k)][k]);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i][0];
  for (int i = 0; i < n; i++) cin >> b[i][0];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 0; i + (1 << j) <= n; i++) {
      a[i][j] = max(a[i][j - 1], a[i + (1 << (j - 1))][j - 1]);
      b[i][j] = min(b[i][j - 1], b[i + (1 << (j - 1))][j - 1]);
    }
  int l, r, maxa, minb, s, e, mid;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    s = inf;
    e = -inf;
    l = i;
    r = n - 1;
    while (l <= r) {
      mid = (l + r) / 2;
      maxa = queryMax(i, mid);
      minb = queryMin(i, mid);
      if (maxa == minb) s = mid;
      if (maxa >= minb)
        r = mid - 1;
      else
        l = mid + 1;
    }
    l = i;
    r = n - 1;
    while (l <= r) {
      mid = (l + r) / 2;
      maxa = queryMax(i, mid);
      minb = queryMin(i, mid);
      if (maxa == minb) e = mid;
      if (maxa > minb)
        r = mid - 1;
      else
        l = mid + 1;
    }
    if (s != inf) sum += (e - s + 1);
  }
  cout << sum << endl;
  return 0;
}
