#include <bits/stdc++.h>
using namespace std;
struct info {
  int minn, val, lazy, maxx;
} tree[4 * 200005];
int arr[200010], brr[200010];
void init(int idx, int b, int e) {
  if (b == e) {
    tree[idx].maxx = arr[b];
    tree[idx].minn = brr[b];
    return;
  }
  init((2 * idx), b, (b + e) / 2);
  init(((2 * idx) + 1), ((b + e) / 2) + 1, e);
  tree[idx].maxx = max(tree[(2 * idx)].maxx, tree[((2 * idx) + 1)].maxx);
  tree[idx].minn = min(tree[(2 * idx)].minn, tree[((2 * idx) + 1)].minn);
}
struct ret {
  int h, l;
};
ret queryMax(int idx, int b, int e, int st, int en) {
  if (st > e || en < b) {
    ret bro;
    bro.h = (long long)-1000000009;
    bro.l = (long long)1000000009;
    return bro;
    ;
  }
  if (st <= b && en >= e) {
    ret bro;
    bro.h = tree[idx].maxx;
    bro.l = tree[idx].minn;
    return bro;
  }
  ret lft, rgt, bruh;
  lft = queryMax((2 * idx), b, (b + e) / 2, st, en);
  rgt = queryMax(((2 * idx) + 1), ((b + e) / 2) + 1, e, st, en);
  bruh.h = max(lft.h, rgt.h);
  bruh.l = min(lft.l, rgt.l);
  return bruh;
}
int fin(int x, int n) {
  int st, en, mid, i, j, a, b, c = 50, d = x, e;
  ret bruh;
  st = x;
  en = n;
  while (st <= en) {
    mid = (st + en) / 2;
    bruh = queryMax(1, 1, n, x, mid);
    a = bruh.h;
    b = bruh.l;
    if (a <= b)
      st = mid + 1;
    else if (a > b)
      en = mid - 1;
    if (a == b) {
      d = max(d, mid);
    }
  }
  return d;
}
int main() {
  ios::sync_with_stdio(false);
  int i, j, n, x, y, a, b, d, st, en, mid, e;
  long long c = 0;
  ret bruh;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> arr[i];
  for (i = 1; i <= n; i++) cin >> brr[i];
  init(1, 1, n);
  for (int i = 1; i <= n; i++) {
    x = fin(i, n);
    bruh = queryMax(1, 1, n, i, x);
    a = bruh.h;
    b = bruh.l;
    if (a == b) {
      d = x;
      st = i;
      en = x;
      while (st <= en) {
        mid = (st + en) / 2;
        bruh = queryMax(1, 1, n, i, mid);
        a = bruh.h;
        b = bruh.l;
        if (a == b) {
          d = min(d, mid);
          en = mid - 1;
        } else
          st = mid + 1;
      }
      e = (x - d + 1);
      c += e;
      j = i + 1;
      while (j <= x) {
        int q, p;
        d = max(j, d);
        bruh = queryMax(1, 1, n, d, x);
        a = bruh.h;
        b = bruh.l;
        bruh = queryMax(1, 1, n, j, d);
        q = bruh.h;
        p = bruh.l;
        if (a == b && q == p) {
          e = x - d + 1;
          c += e;
        } else
          break;
        j++;
      }
      i = j - 1;
    }
  }
  cout << c;
  return 0;
}
