#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int f[N][21], g[N][21], a[N], b[N];
const int INF = 1e9 + 10;
int checka(int l, int r) {
  int ans = -INF;
  for (int i = 20; i >= 0; i--) {
    if ((1 << i) <= r - l + 1) {
      ans = max(ans, f[r][i]);
      r -= (1 << i);
    }
  }
  return ans;
}
int checkb(int l, int r) {
  int ans = INF;
  for (int i = 20; i >= 0; i--) {
    if ((1 << i) <= r - l + 1) {
      ans = min(ans, g[r][i]);
      r -= (1 << i);
    }
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) {
    f[i][0] = a[i];
    for (int j = 1, tmp = 2; j < 20; j++, tmp = tmp * 2)
      if (i >= tmp)
        f[i][j] = max(f[i - tmp / 2][j - 1], f[i][j - 1]);
      else
        f[i][j] = -1;
    g[i][0] = b[i];
    for (int j = 1, tmp = 2; j < 20; j++, tmp = tmp * 2)
      if (i >= tmp)
        g[i][j] = min(g[i - tmp / 2][j - 1], g[i][j - 1]);
      else
        g[i][j] = -1;
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    int l = i, r = n;
    int ans = -1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (checka(i, mid) <= checkb(i, mid)) {
        ans = mid;
        l = mid + 1;
      } else
        r = mid - 1;
    }
    if (ans == -1) continue;
    if (checka(i, ans) != checkb(i, ans)) continue;
    l = i, r = n;
    int xzj = i - 1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (checka(i, mid) < checkb(i, mid)) {
        xzj = mid;
        l = mid + 1;
      } else
        r = mid - 1;
    }
    sum += ans - xzj;
  }
  cout << sum << endl;
  return 0;
}
