#include <bits/stdc++.h>
using namespace std;
int n;
int a[2][200005];
int dp[2][200005][19];
void gao(int c) {
  for (int i = 1; i <= n; ++i) dp[c][i][0] = a[c][i];
  for (int j = 1; (1 << j) <= n; ++j) {
    for (int i = 1; i + (1 << (j - 1)) <= n; ++i) {
      if (c == 0)
        dp[c][i][j] = max(dp[c][i][j - 1], dp[c][i + (1 << (j - 1))][j - 1]);
      if (c == 1)
        dp[c][i][j] = min(dp[c][i][j - 1], dp[c][i + (1 << (j - 1))][j - 1]);
    }
  }
}
int rmq(int l, int r, int c) {
  int k = (int)(log(r - l + 1) / log(2));
  if (c == 0) return max(dp[c][l][k], dp[c][r - (1 << k) + 1][k]);
  return min(dp[c][l][k], dp[c][r - (1 << k) + 1][k]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[0][i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[1][i]);
  gao(0), gao(1);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int l = i, r = n, x, y;
    while (l < r) {
      int m = (l + r) >> 1;
      if (rmq(i, m, 0) < rmq(i, m, 1))
        l = m + 1;
      else
        r = m;
    }
    x = l;
    l = i, r = n;
    while (l < r) {
      int m = (l + r + 1) >> 1;
      if (rmq(i, m, 0) > rmq(i, m, 1))
        r = m - 1;
      else
        l = m;
    }
    y = l;
    if (x == y)
      ans += rmq(i, x, 0) == rmq(i, y, 1) ? 1 : 0;
    else
      ans += max(0, y - x + 1);
  }
  printf("%lld\n", ans);
  return 0;
}
