#include <bits/stdc++.h>
using namespace std;
int a[200005], b[200005], trA[1100005], trB[1100005], n;
int maxx(int a, int b) { return a > b ? a : b; }
int minn(int a, int b) { return a > b ? b : a; }
void build(int i, int l, int r) {
  if (l == r) {
    trA[i] = a[l];
    trB[i] = b[l];
    return;
  } else {
    build(i << 1, l, (l + r) / 2);
    build((i << 1) + 1, (l + r) / 2 + 1, r);
    trA[i] = maxx(trA[i << 1], trA[(i << 1) + 1]);
    trB[i] = minn(trB[i << 1], trB[(i << 1) + 1]);
  }
}
int queryA(int i, int l, int r, int L, int R) {
  if (l == L && r == R) return trA[i];
  if (R <= (l + r) / 2)
    return queryA(i << 1, l, (l + r) / 2, L, R);
  else if (L > (r + l) / 2)
    return queryA((i << 1) + 1, (l + r) / 2 + 1, r, L, R);
  else
    return maxx(queryA(i << 1, l, (l + r) / 2, L, (l + r) / 2),
                queryA((i << 1) + 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, R));
}
int queryB(int i, int l, int r, int L, int R) {
  if (l == L && r == R) return trB[i];
  if (R <= (l + r) / 2)
    return queryB(i << 1, l, (l + r) / 2, L, R);
  else if (L > (r + l) / 2)
    return queryB((i << 1) + 1, (l + r) / 2 + 1, r, L, R);
  else
    return minn(queryB(i << 1, l, (l + r) / 2, L, (l + r) / 2),
                queryB((i << 1) + 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, R));
}
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    int l = i, r = n, ansl = 0, ansr = -1;
    while (l <= r) {
      int mid = (l + r) / 2, prea = queryA(1, 1, n, i, mid),
          preb = queryB(1, 1, n, i, mid);
      if (prea > preb)
        r = mid - 1;
      else if (prea == preb) {
        ansr = mid, l = mid + 1;
      } else
        l = mid + 1;
    }
    l = i, r = n;
    while (l <= r) {
      int mid = (l + r) / 2, prea = queryA(1, 1, n, i, mid),
          preb = queryB(1, 1, n, i, mid);
      if (prea > preb)
        r = mid - 1;
      else if (prea == preb) {
        ansl = mid, r = mid - 1;
      } else
        l = mid + 1;
    }
    sum += ansr - ansl + 1ll;
  }
  printf("%I64d\n", sum);
  return 0;
}
