#include <bits/stdc++.h>
using namespace std;
int tree[800010][2];
int a[200010][2];
int n;
void build(int idx, int l, int r) {
  if (l == r) {
    tree[idx][0] = a[l][0];
    tree[idx][1] = a[l][1];
    return;
  }
  int mid = (l + r) / 2;
  build(idx * 2 + 1, l, mid);
  build(idx * 2 + 2, mid + 1, r);
  tree[idx][0] = max(tree[idx * 2 + 1][0], tree[idx * 2 + 2][0]);
  tree[idx][1] = min(tree[idx * 2 + 1][1], tree[idx * 2 + 2][1]);
}
pair<int, int> get(int idx, int l, int r, int A, int B) {
  if (l > B || r < A) return {-1e9 - 1, 1e9 + 1};
  if (A >= l && B <= r) return {tree[idx][0], tree[idx][1]};
  int mid = (A + B) / 2;
  pair<int, int> p1, p2;
  p1 = get(idx * 2 + 1, l, r, A, mid);
  p2 = get(idx * 2 + 2, l, r, mid + 1, B);
  return {max(p1.first, p2.first), min(p1.second, p2.second)};
}
int main() {
  scanf("%d", &n);
  for (int j = 0; j < 2; j++)
    for (int i = 0; i < n; i++) scanf("%d", &a[i][j]);
  build(0, 0, n - 1);
  long long ans = 0;
  int l, r, m;
  pair<int, int> p;
  int idx, idx2;
  for (int i = 0; i < n; i++) {
    l = i, r = n - 1;
    idx = -1;
    idx2 = -1;
    while (l <= r) {
      m = (l + r) / 2;
      p = get(0, i, m, 0, n - 1);
      if (p.first > p.second)
        r = m - 1;
      else if (p.first < p.second)
        l = m + 1;
      else
        l = m + 1, idx = m;
    }
    l = i, r = n - 1;
    while (l <= r) {
      m = (l + r) / 2;
      p = get(0, i, m, 0, n - 1);
      if (p.first > p.second)
        r = m - 1;
      else if (p.first < p.second)
        l = m + 1;
      else
        r = m - 1, idx2 = m;
    }
    if (idx != -1) ans += idx - idx2 + 1;
  }
  printf("%I64d", ans);
  return 0;
}
