#include <bits/stdc++.h>
using namespace std;
int n;
int size;
int as[200000];
int bs[200000];
int maxseg[19][200000];
int minseg[19][200000];
long long int ans;
int getmin(int l, int r) {
  int len = r - l;
  int k = l;
  int ret = 1000000001;
  for (int i = 18; i >= 0; i--) {
    if ((len >> i) & 1) {
      ret = min(minseg[i][k], ret);
      k += 1 << i;
      len = r - k;
    }
  }
  return ret;
}
int getmax(int l, int r) {
  int len = r - l;
  int k = l;
  int ret = -1000000001;
  for (int i = 18; i >= 0; i--) {
    if ((len >> i) & 1) {
      ret = max(maxseg[i][k], ret);
      k += 1 << i;
      len = r - k;
    }
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 200000; i++) {
    for (int j = 0; j < 19; j++) {
      maxseg[j][i] = -1000000001;
      minseg[j][i] = 1000000001;
    }
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &as[i]);
    maxseg[0][i] = as[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &bs[i]);
    minseg[0][i] = bs[i];
  }
  for (int j = 1; j < 19; j++) {
    for (int i = 0; i < n; i++) {
      int g = i + (1 << (j - 1));
      if (g < n) {
        maxseg[j][i] = max(maxseg[j - 1][i], maxseg[j - 1][g]);
        minseg[j][i] = min(minseg[j - 1][i], minseg[j - 1][g]);
      } else {
        maxseg[j][i] = maxseg[j - 1][i];
        minseg[j][i] = minseg[j - 1][i];
      }
    }
  }
  for (int l = 0; l < n; l++) {
    int low = 0;
    int high = n - l + 1;
    while (high - low > 1) {
      int mid = (low + high) / 2;
      long long int mi = getmin(l, l + mid);
      long long int ma = getmax(l, l + mid);
      long long int t = ma - mi;
      if (t < 0)
        low = mid;
      else
        high = mid;
    }
    int c = low;
    low = 0;
    high = n - l + 1;
    while (high - low > 1) {
      int mid = (low + high) / 2;
      long long int mi = getmin(l, l + mid);
      long long int ma = getmax(l, l + mid);
      long long int t = ma - mi;
      if (t <= 0)
        low = mid;
      else
        high = mid;
    }
    int d = low;
    ans += d - c;
  }
  printf("%lld\n", ans);
}
