#include <bits/stdc++.h>
using namespace std;
const int N = 222222;
long long Max[N][18], Min[N][18];
int a[N], b[N], bit[N], n;
long long Calc(int i, int mid, int k) {
  return max(Max[i][k], Max[mid + 1 - (1 << k)][k]) -
         min(Min[i][k], Min[mid + 1 - (1 << k)][k]);
}
int main() {
  int k = 0;
  bit[1] = 0;
  for (int i = 2; i < N; i++) {
    if (i >= (1 << k + 1)) k++;
    bit[i] = k;
  }
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), Max[i][0] = a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), Min[i][0] = b[i];
  for (int L = 1; L <= bit[n]; L++)
    for (int i = 1; i + (1 << L) - 1 <= n; i++) {
      Max[i][L] = max(Max[i][L - 1], Max[i + (1 << L - 1)][L - 1]);
      Min[i][L] = min(Min[i][L - 1], Min[i + (1 << L - 1)][L - 1]);
    }
  long long Res = 0;
  for (int i = 1; i <= n; i++) {
    int L = i, R = n, mid, Ans1 = i, Ans2 = n;
    while (L <= R) {
      mid = (L + R) >> 1;
      if (Calc(i, mid, bit[mid + 1 - i]) <= 0)
        Ans1 = mid, L = mid + 1;
      else
        R = mid - 1;
    }
    if (Calc(i, Ans1, bit[Ans1 + 1 - i]) != 0) continue;
    L = i, R = n;
    while (L <= R) {
      mid = (L + R) >> 1;
      if (Calc(i, mid, bit[mid + 1 - i]) >= 0)
        Ans2 = mid, R = mid - 1;
      else
        L = mid + 1;
    }
    Res += Ans1 - Ans2 + 1;
  }
  cout << Res << endl;
  return 0;
}
