#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int sta[MAXN][20];
int stb[MAXN][20];
inline int maxlr(int l, int r) {
  int log_len = log(r - l + 1) / log(2);
  return max(sta[l][log_len], sta[r - (1 << log_len) + 1][log_len]);
}
inline int minlr(int l, int r) {
  int log_len = log(r - l + 1) / log(2);
  return min(stb[l][log_len], stb[r - (1 << log_len) + 1][log_len]);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &sta[i][0]);
  for (int i = 1; i <= n; i++) scanf("%d", &stb[i][0]);
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i <= n; i++)
      if (i + (1 << j) - 1 <= n) {
        sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
        stb[i][j] = min(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
      }
  long long ans = 0;
  for (int L = 1; L <= n; L++) {
    int Rl, Rr;
    int l, r;
    Rl = Rr = -1;
    l = L, r = n;
    while (r - l > 5) {
      int mid = (l + r) / 2;
      if (maxlr(L, mid) >= minlr(L, mid))
        r = mid;
      else
        l = mid;
    }
    for (int i = l; i <= r; i++)
      if (maxlr(L, i) == minlr(L, i)) {
        Rl = i;
        break;
      }
    if (Rl == -1) continue;
    l = L, r = n;
    while (r - l > 5) {
      int mid = (l + r) / 2;
      if (maxlr(L, mid) > minlr(L, mid))
        r = mid;
      else
        l = mid;
    }
    for (int i = r; i >= l; i--)
      if (maxlr(L, i) == minlr(L, i)) {
        Rr = i;
        break;
      }
    if (Rr == -1) continue;
    ans += Rr - Rl + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
