#include <bits/stdc++.h>
using namespace std;
int n;
int a[200005];
int b[200005];
int mid(int l, int r) { return (l + (r - l) / 2); }
struct data {
  int maxi;
  int mini;
} seg[800005];
data combine(data x, data y) {
  data z;
  z.maxi = max(x.maxi, y.maxi);
  z.mini = min(x.mini, y.mini);
  return z;
}
void build(int node, int i, int j) {
  if (i == j) {
    seg[node] = (data){a[i], b[i]};
  } else {
    build(2 * node, i, mid(i, j));
    build(2 * node + 1, mid(i, j) + 1, j);
    seg[node] = combine(seg[2 * node], seg[2 * node + 1]);
  }
}
data query(int node, int i, int j, int l, int r) {
  if (i > r || l > j || i > j)
    return (data){INT_MIN, INT_MAX};
  else if (i >= l && j <= r) {
    return seg[node];
  } else {
    return combine(query(2 * node, i, mid(i, j), l, r),
                   query(2 * node + 1, mid(i, j) + 1, j, l, r));
  }
}
int main() {
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) scanf("%d", &b[i]);
  build(1, 1, n);
  long long int megh = 0;
  for (i = 1; i <= n; i++) {
    int L, R;
    L = i;
    R = n;
    int left = 0, right = 0;
    while (L <= R) {
      int midi = mid(L, R);
      data A = query(1, 1, n, i, midi);
      if (A.mini > A.maxi) {
        if (left == 0)
          left = midi;
        else
          left = max(left, midi);
        L = midi + 1;
      } else if (A.mini <= A.maxi) {
        R = midi - 1;
      }
    }
    L = i;
    R = n;
    while (L <= R) {
      int midi = mid(L, R);
      data A = query(1, 1, n, i, midi);
      if (A.maxi > A.mini) {
        if (right == 0)
          right = midi;
        else
          right = min(right, midi);
        R = midi - 1;
      } else if (A.maxi <= A.mini) {
        L = midi + 1;
      }
    }
    if (left == 0 && right == 0)
      megh += (n - i + 1);
    else if (right == 0)
      megh += (n - left);
    else if (left == 0)
      megh += (right - i);
    else {
      left -= (i - 1);
      right -= (i - 1);
      megh += (right - left - 1);
    }
  }
  printf("%lld\n", megh);
  return 0;
}
