#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n;
int mm[maxn];
int a[maxn], b[maxn];
struct ST {
  int dp[maxn][20], ty;
  void build(int n, int b[], int _ty) {
    ty = _ty;
    for (int i = 1; i < int(n + 1); i++) dp[i][0] = ty * b[i];
    for (int j = 1; j <= mm[n]; j++)
      for (int i = 1; i + (1 << j) - 1 <= n; i++)
        dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
  }
  int query(int x, int y) {
    int k = mm[y - x + 1];
    return ty * max(dp[x][k], dp[y - (1 << k) + 1][k]);
  }
} rmq_a, rmq_b;
int main() {
  for (int i = -(mm[0] = -1); i < maxn; i++)
    mm[i] = mm[i - 1] + ((i & (i - 1)) == 0);
  scanf("%d", &n);
  for (int i = 1; i < int(n + 1); i++) scanf("%d", &a[i]);
  for (int i = 1; i < int(n + 1); i++) scanf("%d", &b[i]);
  rmq_a.build(n, a, 1);
  rmq_b.build(n, b, -1);
  long long res = 0;
  for (int i = 1; i < int(n + 1); i++) {
    int l = i, r = n + 1;
    while (l < r) {
      int m = l + (r - l) / 2;
      if (rmq_a.query(i, m) >= rmq_b.query(i, m))
        r = m;
      else
        l = m + 1;
    }
    res -= l;
    l = i, r = n + 1;
    while (l < r) {
      int m = l + (r - l) / 2;
      if (rmq_a.query(i, m) > rmq_b.query(i, m))
        r = m;
      else
        l = m + 1;
    }
    res += l;
  }
  return 0 * printf("%lld\n", res);
}
