#include <bits/stdc++.h>
using namespace std;
const int N = 200000, i = 1 << 19;
int a1[N], a2[N], f1[i], f2[i];
void upd1(int w, int val) {
  w += i >> 1;
  f1[w] = val;
  for (w >>= 1; w; w >>= 1) {
    f1[w] = max(f1[w << 1], f1[(w << 1) + 1]);
  }
}
void upd2(int w, int val) {
  w += i >> 1;
  f2[w] = val;
  for (w >>= 1; w; w >>= 1) {
    f2[w] = min(f2[w << 1], f2[(w << 1) + 1]);
  }
}
int sch(int v) {
  int w = 1, res = 0, t;
  int m1 = -1000000002, m2 = 1000000002;
  for (t = i >> 2; t; t >>= 1) {
    w <<= 1;
    if (max(m1, f1[w]) < min(m2, f2[w]) + v) {
      m1 = max(m1, f1[w]);
      m2 = min(m2, f2[w]);
      w++;
      res += t;
    }
  }
  return res;
}
int main() {
  int q, w, e, r, t;
  cin >> q;
  for (w = 0; w < i; w++) {
    f1[w] = 1000000002;
    f2[w] = -1000000002;
  }
  for (w = 0; w < q; w++) {
    scanf("%d", &a1[w]);
    upd1(w, a1[w]);
  }
  for (w = 0; w < q; w++) {
    scanf("%d", &a2[w]);
    upd2(w, a2[w]);
  }
  long long res = 0;
  for (w = 0; w < q; w++) {
    res += sch(1) - sch(0);
    upd1(w, -1000000002);
    upd2(w, +1000000002);
  }
  cout << res;
  return 0;
}
